实体框架中的错误? .包含和无参数构造函数

发布于 2024-11-30 02:35:24 字数 1873 浏览 6 评论 0原文

简要背景:

首先使用数据库/仅代码(尽管我认为这不重要)

基本设置是这样的:

public class MyContext : DbContext
{
    public DbSet<MyClass_MyClasses> MyClass_MyClasses { get; set; }
    public DbSet<MyClass> MyClasses { get; set; }
}

public class MyClass_MyClasses
{
    [Key]
    [Column(Order = 0)]
    public Guid ParentId { get; set; }

    [Key]
    [Column(Order = 1)]
    public Guid MyClassId { get; set; }

    public int Sequence { get; set; }

    public virtual MyClass MyClass { get; set; }
}

public class MyClass
{
    [Key]
    public Guid Id { get; set; }

    public string Url { get; set; }

    // ...
}

// interface part isn't important, I'm just using the wrapper to
// combine data from different sources to eventually be passed to a Json service
public class EntityWrapper : ISomeInterface
{
    public MyClass_MyClasses Relation { get; set; }

    public string Url { get { return MyClass_MyClasses.MyClass.Url; } }

    // ... some other stuff

    public EntityWrapper() { }
}

问题似乎是当我做类似的事情时 .Include(Func<,>) 没有得到尊重这个:

using(MyContext context = new MyContext())
{

    IEnumerable<EntityWrapper> wrappedResults =
        from relation in context.MyClass_MyClasses.Include(mm => mm.MyClass)
        orderby  relation.Sequence ascending
        select new EntityWrapper { Relation = relation };

    foreach(EntityWrapper wrapper in wrappedResults)
    {
        // always thrown
        if(wrapper.Relation.MyClass == null)
            throw new WtfException("But I specified .Include?");
    }
}

我一直在解决这个问题,不是选择 EntityWrapper,而是选择 MyClass_MyClasses,然后在我的 foreach 中只分配一个局部变量 = new EntityWrapper { ... } 有效。

我的问题是: 我做错了什么吗?或者这是 EF 的一个错误?

另外:提前道歉,这是实际代码的释义,因此有些内容可能不完全相同...如果这不可重现,我会尝试复制我的类的更直接版本。

提前致谢。

Brief background:

Using Database First/Code Only (though I don't think it should matter)

The basic setup is this:

public class MyContext : DbContext
{
    public DbSet<MyClass_MyClasses> MyClass_MyClasses { get; set; }
    public DbSet<MyClass> MyClasses { get; set; }
}

public class MyClass_MyClasses
{
    [Key]
    [Column(Order = 0)]
    public Guid ParentId { get; set; }

    [Key]
    [Column(Order = 1)]
    public Guid MyClassId { get; set; }

    public int Sequence { get; set; }

    public virtual MyClass MyClass { get; set; }
}

public class MyClass
{
    [Key]
    public Guid Id { get; set; }

    public string Url { get; set; }

    // ...
}

// interface part isn't important, I'm just using the wrapper to
// combine data from different sources to eventually be passed to a Json service
public class EntityWrapper : ISomeInterface
{
    public MyClass_MyClasses Relation { get; set; }

    public string Url { get { return MyClass_MyClasses.MyClass.Url; } }

    // ... some other stuff

    public EntityWrapper() { }
}

The problem seems to be that .Include(Func<,>) is not being honored when I do something like this:

using(MyContext context = new MyContext())
{

    IEnumerable<EntityWrapper> wrappedResults =
        from relation in context.MyClass_MyClasses.Include(mm => mm.MyClass)
        orderby  relation.Sequence ascending
        select new EntityWrapper { Relation = relation };

    foreach(EntityWrapper wrapper in wrappedResults)
    {
        // always thrown
        if(wrapper.Relation.MyClass == null)
            throw new WtfException("But I specified .Include?");
    }
}

I've been working around it by instead of selecting to the EntityWrapper, selecting to the MyClass_MyClasses and then in my foreach just assigning a local variable = new EntityWrapper { ... } which works.

My question is:
Am I doing something incorrectly? or is this a bug with EF?

Also: apologies in advance, this is a paraphrase of actual code so some of the things might not be exactly the same... If this isn't reproducible I'll try and copy more direct versions of my classes.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

第几種人 2024-12-07 02:35:24

Include 和投影是分离的=一旦使用投影,Include 就不会自动使用。试试这个:

var results =
    from relation in context.MyClass_MyClasses.Include(mm => mm.MyClass)
    orderby  relation.Sequence ascending
    select relation;

foreach(EntityWrapper wrapper in results.ToList()
                                        .Select(r => new EntityWrapper { Relation = r }))
{
    // always thrown
    if(wrapper.Relation.MyClass == null)
        throw new WtfException("But I specified .Include?");
}

您还可以尝试使用 AsEnumerable 而不是 ToList

Include and projection are disjunctive = once you use projection Include is not used automatically. Try this:

var results =
    from relation in context.MyClass_MyClasses.Include(mm => mm.MyClass)
    orderby  relation.Sequence ascending
    select relation;

foreach(EntityWrapper wrapper in results.ToList()
                                        .Select(r => new EntityWrapper { Relation = r }))
{
    // always thrown
    if(wrapper.Relation.MyClass == null)
        throw new WtfException("But I specified .Include?");
}

You can also try to use AsEnumerable instead of ToList

请帮我爱他 2024-12-07 02:35:24

尝试删除virtual关键字。

public virtual MyClass MyClass { get;放; }

try to remove the virtual keyword.

public virtual MyClass MyClass { get; set; }

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