gpt4 book ai didi

c# - 抽象类型 X 没有映射后代,因此无法映射

转载 作者:太空狗 更新时间:2023-10-29 17:54:55 24 4
gpt4 key购买 nike

我有以下模型:

public abstract class AbstractBase { }
public abstract class AbstractBase<T> : AbstractBase where T : SomeOtherTypeBase
{
T MyProp {get; set;}
}
public class Concrete1 : AbstractBase<OtherTypeSpecializationFor1> { }
public class Concrete2 : AbstractBase<OtherTypeSpecializationFor2> { }

但是 Entity Framework 给我错误:

The abstract type AbstractBase has no mapped descendants and so cannot be mapped

在我看来,这不应该发生,因为 AbstractBase 直接继承自 AbstractBase,而具体的类 Concrete1/2 继承自 GenericAbstractBase。这是怎么回事?

此外,出于好奇,我想知道 GenericAbstractBase 中类型 T 的属性是否会被 EF 保留,以防路过的人想到答案。

更新 1

任何人都可以确认这是 EF 支持的吗?我看过这个post根据罗文的回答,情况应该如此。谢谢

更新 2当通用基类不是抽象的时,同样的问题。

最佳答案

这是答案 from the EF Team :

EDM, the underlying meta-model used by the EF6 runtime to reason about the entity types does not support generics. We allow non-generic entity types that inherit from common generic types to be added to models but as we walk up the inheritance hierarchies we stop looking as soon as we hit a generic ancestor. You typically use this capability to model the shape of your entity types using the full expressiveness of inheritance and generics while on the CLR side of things, however on the EF side this results in unrelated entity types being added.

That way you have defined your DbContext, from the perspective of the EF6 runtime you are adding three different and unrelated entity types: AbstractBase, Concrete1 and Concrete2. All the generic types in the middle of the hierarchy have been ignored and therefore EF does not know that they are related.

With that limitation in mind, the exception you are getting is expected, since AbstractBase is abstract and does not have any concrete descendants know to EF. If you add a separate non-generic and concrete type that inherits directly from AbstractBase, e.g.:

public class ConcreteFork : AbstractBase {  }

Your model should be valid again. However you won't be able to use MyContext.AbstractBases to bootstrap queries that return instances of Concrete1 or Concrete2, as EF is unaware of the fact that they are related.

By the way, in EF7 we got rid of the EDM layer for the implementation of EF and we expect to be able to support more and more scenarios for actual generic entity types.

Hope this helps explain what is going on.

Diego

关于c# - 抽象类型 X 没有映射后代,因此无法映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29197744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com