C# 4.0 EF代码优先-多对多
我在首先设计代码中的多对多关系时遇到问题。EF正在创建连接表并按照我的预期关联Fk,但是当我尝试访问用户的MailingList集合时,没有条目 我已经通过种子设定实现了初始化测试数据,数据都存在于数据库中 我认为问题在于用户和邮件列表的构造函数,但我不确定。我希望能够浏览User.MailingLists的导航属性C# 4.0 EF代码优先-多对多,c#-4.0,ef-code-first,entity-framework-5,C# 4.0,Ef Code First,Entity Framework 5,我在首先设计代码中的多对多关系时遇到问题。EF正在创建连接表并按照我的预期关联Fk,但是当我尝试访问用户的MailingList集合时,没有条目 我已经通过种子设定实现了初始化测试数据,数据都存在于数据库中 我认为问题在于用户和邮件列表的构造函数,但我不确定。我希望能够浏览User.MailingLists的导航属性 var user = db.Users.Find(1); Console.WriteLine("{0}", user.EmailAddress); //This
var user = db.Users.Find(1);
Console.WriteLine("{0}", user.EmailAddress); //This is Fine
Console.WriteLine("{0}", user.Address.PostCode); /This is Fine
foreach (MailingList ml in user.MailingLists) // this is 0
{
Console.WriteLine("{0}", ml.Name);
}
我的模型如下:-
public class User : IEntityBase
{
public User()
{
MailingLists = new List<MailingList>();
}
public int Id { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string EmailAddress { get; set; }
public DateTime? DateLastUpdated { get; set; }
public DateTime DateCreated { get; set; }
public bool IsDeleted { get; set; }
public virtual Address Address { get; set; }
public ICollection<MailingList> MailingLists { get; set; }
}
public class MailingList : IEntityBase
{
public MailingList()
{
Users = new List<User>();
}
public int Id { get; set; }
public string Name { get; set; }
public DateTime? DateLastUpdated { get; set; }
public DateTime DateCreated { get; set; }
public bool IsDeleted { get; set; }
public ICollection<User> Users { get; set; }
}
public class Address : IEntityBase
{
public int Id { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string City { get; set; }
public string County { get; set; }
public string PostCode { get; set; }
public DateTime? DateLastUpdated { get; set; }
public DateTime DateCreated { get; set; }
public bool IsDeleted { get; set; }
}
公共类用户:IEntityBase
{
公共用户()
{
MailingLists=新列表();
}
公共int Id{get;set;}
公共字符串名{get;set;}
公共字符串姓氏{get;set;}
公共字符串电子邮件地址{get;set;}
公共日期时间?DateLastUpdated{get;set;}
public DateTime DateCreated{get;set;}
公共布尔被删除{get;set;}
公共虚拟地址{get;set;}
公共ICollection邮件列表{get;set;}
}
公共类邮件列表:IEntityBase
{
公共邮件列表()
{
用户=新列表();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共日期时间?DateLastUpdated{get;set;}
public DateTime DateCreated{get;set;}
公共布尔被删除{get;set;}
公共ICollection用户{get;set;}
}
公共类地址:IEntityBase
{
公共int Id{get;set;}
公共字符串AddressLine1{get;set;}
公共字符串AddressLine2{get;set;}
公共字符串AddressLine3{get;set;}
公共字符串City{get;set;}
公共字符串country{get;set;}
公共字符串邮政编码{get;set;}
公共日期时间?DateLastUpdated{get;set;}
public DateTime DateCreated{get;set;}
公共布尔被删除{get;set;}
}
欢迎任何建议。您既不急于加载带有查询的
邮件列表
条目,也不满足延迟加载代理的要求,因此EF无法填充集合
要允许延迟加载,请将MailingList
属性更改为虚拟,以允许EF代理覆盖它
要使用即时加载,请在查询中使用
Include()
(System.Data.Entity
中的扩展方法)指定应加载MailingList
。谢谢,这里的首选方法是什么?急切加载的扩展性是否较小?如果您确定需要数据,那么急切加载会更好,因为它将在对数据库的一次调用中加载所有内容。如果您不知道需要什么数据,那么延迟加载会更好。在性能不成问题的情况下,它也很方便。因此,通过快速加载,我可以在这样的基础上工作:如果我考虑到我要找的东西,我的调用只能从特定对象获得我需要的东西。如果我需要抓取与实体相关的所有数据,那么延迟加载会拉入所有数据吗?