实体框架中的错误? .包含和无参数构造函数
简要背景:
首先使用数据库/仅代码(尽管我认为这不重要)
基本设置是这样的:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Include
和投影是分离的=一旦使用投影,Include
就不会自动使用。试试这个:您还可以尝试使用
AsEnumerable
而不是ToList
Include
and projection are disjunctive = once you use projectionInclude
is not used automatically. Try this:You can also try to use
AsEnumerable
instead ofToList
尝试删除
virtual
关键字。public virtual MyClass MyClass { get;放; }
try to remove the
virtual
keyword.public virtual MyClass MyClass { get; set; }