为什么我的EF集合不懒惰?
我首先使用 EF 4.1 代码。我有一个用户模型和一个设置模型。
每次存储库返回用户时,也会加载设置。我已将设置标记为虚拟,所有访问修饰符均为 public LazyLoadingEnabled 和 ProxyCreationEnabled 默认启用。
我缺少什么?
public class User : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public virtual ICollection<Setting> Settings { get; set; }
}
public class Setting
{
public int UserID { get; set; }
public int SettingID { get; set; }
public string Value { get; set; }
}
用户可能有多个设置,因此设置中与外键存在一对多关系。
用户配置为
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
HasKey(u => u.ID);
HasMany(u => u.Settings).WithOptional().HasForeignKey(u => u.UserID);
}
}
,设置配置为:
public class SettingsConfiguration : EntityTypeConfiguration<Setting>
{
public SettingsConfiguration()
{
ToTable("UserSettings");
HasKey(s => new { s.UserID, s.SettingID });
}
}
I'm using EF 4.1 Code first. I have a model of user and a model of setting.
Each time the repository returns a user the Setting is also loaded. I've marked the Setting as virtual all my access modifiers are public LazyLoadingEnabled and ProxyCreationEnabled are enabled by default.
What am I missing?
public class User : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public virtual ICollection<Setting> Settings { get; set; }
}
public class Setting
{
public int UserID { get; set; }
public int SettingID { get; set; }
public string Value { get; set; }
}
The User might have several setting, so there is a one to many relationship with a foreign key in setting.
The user configuration is
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
HasKey(u => u.ID);
HasMany(u => u.Settings).WithOptional().HasForeignKey(u => u.UserID);
}
}
and the Setting configuration is:
public class SettingsConfiguration : EntityTypeConfiguration<Setting>
{
public SettingsConfiguration()
{
ToTable("UserSettings");
HasKey(s => new { s.UserID, s.SettingID });
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
延迟加载的含义与您想象的相反。
查询
用户
时,不会立即检索设置
首次访问时会检索
设置
。DbContext
必须在此时打开才能发生这种情况;否则你会得到一个例外查询
用户
时,不会立即检索设置
设置
将永远自动检索;它将返回 null (在我看来,这是一个糟糕的设计决策:null 是一个错误的值,你不应该获得它)在这两种情况下,您可以使用
.Include(x => x.Settings)
立即加载Settings
,或者在需要时调用context.Entry(user)。 Collection(x => x.Settings).Load()
Lazy loading means the opposite of what you think it means.
Settings
is not retrieved immediately when queryingUser
Settings
is retrieved when it's accessed for the first time. TheDbContext
must be open at that time for this to happen; otherwise you'll get an exceptionSettings
is not retrieved immediately when queryingUser
Settings
will never be retrieved automatically; it will return null (which, in my opinion, is a terrible design decision: null is a wrong value and you shouldn't be able to get it)In both cases, you can load
Settings
eagerly by using.Include(x => x.Settings)
, or when needed, by callingcontext.Entry(user).Collection(x => x.Settings).Load()