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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/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
Entity framework 具有多个键的实体框架多对多关系_Entity Framework_Many To Many_Code First - Fatal编程技术网

Entity framework 具有多个键的实体框架多对多关系

Entity framework 具有多个键的实体框架多对多关系,entity-framework,many-to-many,code-first,Entity Framework,Many To Many,Code First,我想添加一个表来定义权限对象,它可以应用于许多模型。 为此,我创建了一个权限类: public class Permission { [Key] int PermissionID {get; set;} string User {get; set;} bool Read {get; set;} bool Write {get; set;} } 然后是其他可以拥有权限列表的对象类: public class ObjectModel1 { [Key]

我想添加一个表来定义权限对象,它可以应用于许多模型。 为此,我创建了一个权限类:

public class Permission
{
    [Key]
    int PermissionID {get; set;}
    string User {get; set;}
    bool Read {get; set;}
    bool Write {get; set;}
}
然后是其他可以拥有权限列表的对象类:

public class ObjectModel1
{
    [Key]
    int idObject1 {get; set;}
    ... Other properties ...
    public virtual ICollection<Permission> Permission {get; set;}
}

public class ObjectModel2
{
    [Key]
    int idObject2 {get; set;}
    ... Other properties ...
    public virtual ICollection<Permission> Permission {get; set;}
}
公共类ObjectModel1
{
[关键]
int idObject1{get;set;}
…其他属性。。。
公共虚拟ICollection权限{get;set;}
}
公共类ObjectModel2
{
[关键]
int idObject2{get;set;}
…其他属性。。。
公共虚拟ICollection权限{get;set;}
}

在权限类中为每个对象定义外键的情况下,如何在权限和其他对象类之间获得多对多关系?

多对多关系将在两个实体之间具有外部参照表:

// because Permission has a collection to ObjectModel1 and ObjectModel1 has a collection
// to permission, it is treated as a many to many relationship with an implicit
// xref between the tables. The xref will contain a foreign key to each entity that is
// also a composite primary key
public class Permission
{
    [Key]
    int PermissionID {get; set;}
    string User {get; set;}
    bool Read {get; set;}
    bool Write {get; set;}

    public virtual ICollection<ObjectModel1> ObjectModel1s { get; set; }
    public virtual ICollection<ObjectModel2> ObjectModel2s { get; set; }
}

public class ObjectModel1
{
    [Key]
    int idObject1 {get; set;}
    ... Other properties ...
    public virtual ICollection<Permission> Permission {get; set;}
}

public class ObjectModel2
{
    [Key]
    int idObject2 {get; set;}
    ... Other properties ...
    public virtual ICollection<Permission> Permission {get; set;}
}
//因为权限有一个到ObjectModel1的集合,而ObjectModel1有一个集合
//对于权限,它被视为具有隐式
//表之间的外部参照。外部参照将包含每个实体的外键
//也是一个复合主键
公共类许可
{
[关键]
int PermissionID{get;set;}
字符串用户{get;set;}
bool Read{get;set;}
bool Write{get;set;}
公共虚拟ICollection ObjectModel1s{get;set;}
公共虚拟ICollection ObjectModel2s{get;set;}
}
公共类ObjectModel1
{
[关键]
int idObject1{get;set;}
…其他属性。。。
公共虚拟ICollection权限{get;set;}
}
公共类ObjectModel2
{
[关键]
int idObject2{get;set;}
…其他属性。。。
公共虚拟ICollection权限{get;set;}
}
实体框架将创建一个类似PermissionObjectModel1的表,该表有一个复合主键和两个外键(一个到Permission,一个到ObjectModel1)。它将为ObjectModel2创建另一个具有类似键的表。权限本身不存在外键

如果您不想在权限上使用navigation属性,那么我认为您需要使用Fluent API:

public class MyDbContext : DbContext
{
    public DbSet<Permission> Permissions { get; set; }
    public DbSet<ObjectModel1> ObjectModel1s { get; set; }
    public DbSet<ObjectModel2> ObjectModel2s { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ObjectModel1>()
            .HasMany(many => many.Permissions)
            .WithMany() // dont want navigation property on Permission
            .Map(xref => xref.MapLeftKey("ObjectModel1Id")
                             .MapRightKey("PermissionId")
                             .ToTable("ObjectModel1PermissionXref"));

        modelBuilder.Entity<ObjectModel2>()
            .HasMany(many => many.Permissions)
            .WithMany() // dont want navigation property on Permission
            .Map(xref => xref.MapLeftKey("ObjectModel2Id")
                             .MapRightKey("PermissionId")
                             .ToTable("ObjectModel2PermissionXref"));
    }
}
公共类MyDbContext:DbContext
{
公共数据库集权限{get;set;}
公共DbSet ObjectModel1s{get;set;}
公共DbSet ObjectModel2s{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(many=>many.Permissions)
.WithMany()//不需要权限上的导航属性
.Map(xref=>xref.MapLeftKey(“ObjectModel1Id”)
.MapRightKey(“许可ID”)
.ToTable(“ObjectModel1PermissionXref”);
modelBuilder.Entity()
.HasMany(many=>many.Permissions)
.WithMany()//不需要权限上的导航属性
.Map(xref=>xref.MapLeftKey(“ObjectModel2Id”)
.MapRightKey(“许可ID”)
.ToTable(“ObjectModel2PermissionXref”);
}
}
类似于上述代码的内容仍然会为您提供多对多关系,但不会在权限上定义导航属性