تبدو مجموعاتي / نموذجيي جيدة بعد الحفظ ، ولكن عند تحميل الكيان لا أحصل على البيانات مرة أخرى.ماذا أفعل خطأ?

StackOverflow https://stackoverflow.com//questions/9689539

سؤال

هنا هو بلدي شفرة المصدر لنموذج بلدي:

public class User
{
    public User()
    {
        GUID = Guid.NewGuid();
        Account = new Account();
        Location = new Location();
    }

    public long UserID { get; set; }
    public Guid GUID { get; set; }
    public string DisplayName { get; set; }
    public Location Location { get; set; }
    public virtual Account Account { get; set; }
}

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        HasKey(x => x.UserID);
    }
}

[ComplexType]
public class Location
{
    [MaxLength(2)]
    public string CountryCode { get; set; }
    [MaxLength(2)]
    public string StateCode { get; set; }
    public string City { get; set; }
}

public class Account
{
    public Account()
    {
        if (EmailAddresses == null) EmailAddresses = new Collection<EmailAddress>();
    }

    [ForeignKey("User")]
    public long AccountID { get; set; }

    public ICollection<EmailAddress> EmailAddresses { get; set; }

    public virtual User User { get; set; }
}

public class AccountConfiguration : EntityTypeConfiguration<Account>
{
    public AccountConfiguration()
    {
        HasKey(x => x.AccountID);
        HasMany(x => x.EmailAddresses).WithRequired(x => x.Account);
    }
}

public class EmailAddress
{
    [Key]
    public string Email { get; set; }

    public EmailTypes Type { get; set; }

    public long AccountID { get; set; }
    public virtual Account Account { get; set; }
}

public class EmailAddressConfiguration : EntityTypeConfiguration<EmailAddress>
{
    public EmailAddressConfiguration()
    {
        HasKey(x => x.Email);
        HasRequired(x => x.Account).WithMany(x => x.EmailAddresses).HasForeignKey(x => x.AccountID);
    }
}

وهنا هو بلدي فئة الكيان:

public class MyEntities : DbContext
{
    public MyEntities()
    {
        Database.SetInitializer<MyEntities>(new DropCreateDatabaseAlways<MyEntities>());
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Account> Accounts { get; set; }
    public DbSet<EmailAddress> EmailAddresses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new UserConfiguration());
        modelBuilder.Configurations.Add(new AccountConfiguration());
        modelBuilder.Configurations.Add(new EmailAddressConfiguration());

        base.OnModelCreating(modelBuilder);
    }
}

وأخيرا رمز بلدي الذي يعمل في تطبيق وحدة التحكم اختبار:

static void Main(string[] args)
    {
        var id = CreateUser();

        using (MyEntities db = new MyEntities())
        {
            var a = db.Users.Find(id);
            var b = a.Account.EmailAddresses;
            var c = db.Accounts.Find(id);
            var d = db.EmailAddresses.Where(x => x.Account.AccountID == id).ToList();
        }

    }

    private static long CreateUser()
    {
        using (MyEntities db = new MyEntities())
        {
            var u = new User();
            u.DisplayName = "TEST";

            u.Location.CountryCode = "US";
            u.Location.StateCode = "HI";
            u.Location.City = "Kauai";

            EmailAddress e = new EmailAddress();
            e.Email = DateTime.UtcNow.Ticks + "@microsoft.com";
            e.Type = EmailTypes.Current;

            u.Account.EmailAddresses.Add(e);

            db.Users.Add(u);

            var cnt = db.SaveChanges();
            // Here I get a return of the 4 entities saved, and my model looks correct.

            return u.UserID;
        }
    }

بمجرد حفظ النموذج (إنشاءالمستخدم) ، تمكنت من التنقل في النموذج وبدا كل شيء مثاليا.

تنشأ المشكلة عندما أحاول سحب البيانات مرة أخرى.
المتغيرات الخاصة بي:

أ navigating يظهر الانتقال إلى البريد الإلكتروني 0 سجلات.

ب shows هذا يظهر أيضا 0 سجلات في المجموعة.

ج navigating الانتقال إلى البريد الإلكتروني يظهر الأديرس 0 السجلات.

هنا يمكنني الحصول على عناوين البريد الإلكتروني (ولكن ليس عن طريق التنقل في النموذج)

هل كانت مفيدة؟

المحلول

يعتمد رمز الاختبار الخاص بك للوصول إلى خصائص التنقل على التحميل البطيء.ولكن الخاص بك Account.EmailAddresses لم يتم وضع علامة على المجموعة على أنها virtual:

public ICollection<EmailAddress> EmailAddresses { get; set; }

يجب أن تكون خصائص التنقل virtual (مثل الخاص بك User.Account الملكية) من أجل جعل تحميل كسول ممكن.

كملاحظة جانبية:أوصي لإزالة مثيل من Account خاصية الملاحة...

Account = new Account();

...من ال User منشئ.هذا هو مصدر معروف عن المتاعب:

ما من شأنه أن يسبب إطار الكيان لحفظ تفريغ (ولكن كسول للتحميل) مرجع على البيانات الموجودة?

إف 4.1 كود أولا:لماذا لا تقوم إي إف بتعيين خاصية التنقل هذه?

إنشاء مثيل Location على ما يرام لأنه نوع معقد وليس خاصية الملاحة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top