Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF 4.0基于泛型的继承_C#_Entity Framework_Entity Framework 4 - Fatal编程技术网

C# EF 4.0基于泛型的继承

C# EF 4.0基于泛型的继承,c#,entity-framework,entity-framework-4,C#,Entity Framework,Entity Framework 4,我有一节这样的课 public abstract class BaseType<T> { public string Name {}; public T TypedValue { get { return GetTypedValue(PersistedValue); } }; public string PersistedValue {} public abstract T GetTypedValue(Persiste

我有一节这样的课

public abstract class BaseType<T>
{
  public string Name {};
  public T TypedValue { 
    get {
       return GetTypedValue(PersistedValue);
        }  
  };
  public string PersistedValue {} 
  public abstract T GetTypedValue(PersistedValue);
}
公共抽象类基类
{
公共字符串名称{};
公共T类型值{
得到{
返回GetTypedValue(PersistedValue);
}  
};
公共字符串PersistedValue{}
公共摘要T GetTypedValue(PersistedValue);
}
然后许多派生类

public class IntegerType:BaseType<int>
{
 ...
}
公共类IntegerType:BaseType
{
...
}
是否可以使用EF4.0使用表/继承方案映射此层次结构? 当前生成的代码创建有一个错误,因为它生成一个类似

public <T> ObjectSet<TypedAttribute<T>> TypedAttributes
{
     get 
     { 
        return _typedAttributes  ?? (_typedAttributes = CreateObjectSet<TypedAttribute<T>>("TypedAttributes")); }
     }
private ObjectSet<TypedAttribute> _typedAttributes;
公共对象集类型属性
{
得到
{ 
返回_typedAttributes???(_typedAttributes=CreateObjectSet(“typedAttributes”);}
}
私有对象集_typedAttributes;

我不这么认为,因为:

  • 继承映射要求基类是EDMX中的实体
  • 使用继承时,
    ObjectSet
    用于基本类型。当必须使用
    ObjectSet
    来检索任何子类型时,您会使用哪个泛型参数来创建它的实例
它可以在不继承的情况下部分实现(至少对于POCO)。只需在EDMX中对您的子类型进行建模,而不使用基类型。然后手动创建POCO类并从泛型基类型派生它们。您必须遵循的唯一规则是,POCO类必须与EDMX中的实体具有相同的名称,并且必须在EDMX中设置具有可访问性的所有属性。如果要使用更改跟踪属性,则必须将其标记为虚拟。如果要使用延迟加载,导航属性也必须是虚拟的

例如:

假设我在EDMX中有两个实体:IntegerValue和DoubleValue。现在,我将这些POCO定义如下:

public abstract class BaseType<T>
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual T Value { get; set; }
}

public class IntegerValue : BaseType<int>
{ }

public class DoubleValue : BaseType<double>
{ }
公共抽象类基类
{
公共虚拟整数Id{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟T值{get;set;}
}
公共类IntegerValue:BaseType
{ }
公共类DoubleValue:BaseType
{ }

它将导致每个子类型有一个表。

您是正确的,因为objectset在声明时需要一个类型,它没有任何用处,我已经在一个单独的接口中分离了泛型功能。谢谢