1

这是我的模型的源代码:

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;
        }
    }

一旦模型被保存(CreateUser),我就可以浏览模型,一切看起来都很完美。

当我尝试将数据拉回时,就会出现问题。
我的变量:

a -- 导航到电子邮件地址显示 0 条记录。

b - 这也显示集合中的 0 条记录。

c -- 导航到电子邮件地址显示 0 条记录。

d -- 在这里我可以获得电子邮件地址(但不能通过导航模型)

4

1 回答 1

0

您访问导航属性的测试代码依赖于延迟加载。但您的Account.EmailAddresses收藏未标记为virtual

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

导航属性必须virtual(如您的User.Account属性)才能使延迟加载成为可能。

作为旁注:我建议删除Account导航属性的实例化......

Account = new Account();

...来自User构造函数。这是一个已知的麻烦来源:

什么会导致实体框架在现有数据上保存未加载(但可延迟加载)的引用?

EF 4.1 Code First:为什么 EF 不设置此导航属性?

实例化Location很好,因为它是一种复杂类型而不是导航属性。

于 2012-03-13T18:42:22.087 回答