Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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_Multiple Columns_Code First - Fatal编程技术网

C# 继承的非重写字段上的多列索引

C# 继承的非重写字段上的多列索引,c#,entity-framework,inheritance,multiple-columns,code-first,C#,Entity Framework,Inheritance,Multiple Columns,Code First,我有一个具有一些属性和行为的基类。这个基类由许多其他类扩展/继承。其中一些类应该在它们自己的一个属性和基类的一个属性上创建唯一的多列索引 public class BaseClass { long employeeId {get; set;} // and many other things... } public class Buzzword : BaseClass { string Name {get;set;} // supposed to be unique

我有一个具有一些属性和行为的基类。这个基类由许多其他类扩展/继承。其中一些类应该在它们自己的一个属性和基类的一个属性上创建唯一的多列索引

public class BaseClass
{ 
    long employeeId {get; set;}
    // and many other things...
}

public class Buzzword : BaseClass
{
    string Name {get;set;} // supposed to be unique for every employee
    // many other things...
}
我现在想重复我的Buzzword类:

public class Buzzword : BaseClass
{
    [Index("IX_Buzzword_EmployeeId_Name", IsUnique = true, Order = 1]
    // black magic: inherited property of BaseClass
    [Index("IX_Buzzword_EmployeeId_Name", IsUnique = true, Order = 2]
    string Name {get;set;} // supposed to be unique for every employee
    // many other things...
}
我该怎么做?使employeeId为虚拟(因此仍在所有子类中实现),并在类中重写它以进行多列索引定义(以及对基实现的调用)

亲切问候,,
mate

如果基类包含多列索引中所需的列,则必须跳过使用注释,并对映射使用
EntityTypeConfiguration

因此,在DbContext中,可以执行以下操作:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BuzzWord>().Property(b => b.EmployeeId).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_Buzzword_EmployeeId_Name", 1)));
        modelBuilder.Entity<BuzzWord>().Property(b => b.Name).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_Buzzword_EmployeeId_Name", 2)));
        base.OnModelCreating(modelBuilder);
    } 
protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //  This should include any mappings defined in the same assembly as the BuzzWordMapping class
        modelBuilder.Configurations.AddFromAssembly(typeof(BuzzWordMapping).Assembly);
        base.OnModelCreating(modelBuilder);            
    }

请阅读并考虑修改您的标题删除标签:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //  This should include any mappings defined in the same assembly as the BuzzWordMapping class
        modelBuilder.Configurations.AddFromAssembly(typeof(BuzzWordMapping).Assembly);
        base.OnModelCreating(modelBuilder);            
    }