为什么我的EF集合不懒惰?

发布于 2024-11-13 07:40:28 字数 1142 浏览 4 评论 0原文

我首先使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-11-20 07:40:28

延迟加载的含义与您想象的相反。

  • 使用延迟加载(虚拟属性和默认值)
      查询用户时,不会立即检索

    • 设置
    • 首次访问时会检索

    • 设置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.

  • With lazy loading (virtual property and defaults)
    • Settings is not retrieved immediately when querying User
    • Settings is retrieved when it's accessed for the first time. The DbContext must be open at that time for this to happen; otherwise you'll get an exception
  • Without lazy loading (non-virtual property and/or explicitly disabled)
    • Settings is not retrieved immediately when querying User
    • 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 calling context.Entry(user).Collection(x => x.Settings).Load()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文