Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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# 如何继承实体框架配置类(带集合)_C#_Entity Framework_Inheritance_Configuration_Ef Code First - Fatal编程技术网

C# 如何继承实体框架配置类(带集合)

C# 如何继承实体框架配置类(带集合),c#,entity-framework,inheritance,configuration,ef-code-first,C#,Entity Framework,Inheritance,Configuration,Ef Code First,我们正在使用实体框架6来形成我们的DAL。对于应用程序的某一部分,我们尝试重用“标准模型”的很大一部分。我们在那里扩展了一些类(“Aaa”->“AaaSp”),以便在新模型中使用,并向它们添加关联。(见图)。特殊中定义的类以及以“Aaa”形式引用的类用于创建新的DbContext(SpContext)。 问题是将Bbb集合的配置添加到Aaa类。在SpecialDb中,我们希望使用“TablePerType”,因此我们不会得到带有无用鉴别器的“TPH”。要进行配置,请在“AaaSp”的配置类中继承

我们正在使用实体框架6来形成我们的DAL。对于应用程序的某一部分,我们尝试重用“标准模型”的很大一部分。我们在那里扩展了一些类(“Aaa”->“AaaSp”),以便在新模型中使用,并向它们添加关联。(见图)。特殊中定义的类以及以“Aaa”形式引用的类用于创建新的DbContext(SpContext)。 问题是将Bbb集合的配置添加到Aaa类。在SpecialDb中,我们希望使用“TablePerType”,因此我们不会得到带有无用鉴别器的“TPH”。要进行配置,请在“AaaSp”的配置类中继承“ConfigAaa”

代码等级“Aaa”:

公共类Aaa:实体
{
公共图书馆
{
获取{return bbbs;}
设置{bbbs=value;}
}
私有IList bbbs=新列表();
公共类ConfigAaa:EntityConfiguration库
{
公共配置AAA()
{
HasMany(a=>a.Bbbs)。WithMany(b=>b.Aaas);
}
}
}
为此,“ConfigAaa”需要像“EntityConfigurationBase”一样通用。我们试过这样的方法:

public class Aaa : Entity
{

    public IList<Bbb> Bbbs
    {
        get { return bbbs; }
        set { bbbs = value; }
    }
    private IList<Bbb> bbbs = new List<Bbb>();

    public class ConfigAaa<T>:EntityConfigurationBase<T> where T: Aaa

    {
        public ConfigAaa()
        {
            HasMany(a => a.Bbbs).WithMany(b => b.Aaas);
            //Error:  collection Aaas is not of Type T                  
        }
    }
}
公共类Aaa:实体
{
公共图书馆
{
获取{return bbbs;}
设置{bbbs=value;}
}
私有IList bbbs=新列表();
公共类ConfigAaa:EntityConfiguration库,其中T:Aaa
{
公共配置AAA()
{
HasMany(a=>a.Bbbs)。WithMany(b=>b.Aaas);
//错误:集合Aaas不是T类型
}
}
}
但是,一旦执行此操作,就会在“Aaa”的集合属性上出现错误。无法转换表达式类型“System.Collections.Generic.IList”。尽管“where”约束将“T”限制为“Aaa”或后代

有没有办法让这一切顺利进行

public class Aaa : Entity
{

    public IList<Bbb> Bbbs
    {
        get { return bbbs; }
        set { bbbs = value; }
    }
    private IList<Bbb> bbbs = new List<Bbb>();

    public class ConfigAaa<T>:EntityConfigurationBase<T> where T: Aaa

    {
        public ConfigAaa()
        {
            HasMany(a => a.Bbbs).WithMany(b => b.Aaas);
            //Error:  collection Aaas is not of Type T                  
        }
    }
}