Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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/0/asp.net-core/3.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#_Asp.net Core_Entity Framework Core - Fatal编程技术网

C# 实体框架核心中同一实体的多对多关系

C# 实体框架核心中同一实体的多对多关系,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,当我实现两个类之间的多对多关系,并使用fluent api配置其复合主键时,EF Core不会在数据库中创建两列,而是创建三列 UserReport.cs public class UserReports { public int ReporterId { get; set; } public User Reporter { get; set; } public int ReporteeId { get; set; } public User Reportee

当我实现两个类之间的多对多关系,并使用fluent api配置其复合主键时,EF Core不会在数据库中创建两列,而是创建三列

UserReport.cs

public class UserReports
{
    public int ReporterId { get; set; }
    public User Reporter { get; set; }

    public int ReporteeId { get; set; }
    public User Reportee { get; set; }
}
public class User: IdentityUser<int>
{
    public DateTime DateOfBirth { get; set; }
    public string KnownAs { get; set; }
    public DateTime Created { get; set; }
    public DateTime LastActive { get; set; }

    public ICollection<UserReports> Reporters { get; set; } = new Collection<UserReports>();
    public ICollection<UserReports> Reportees { get; set; } = new Collection<UserReports>();
}
modelBuilder.Entity<UserReports>()
   .HasKey(ru => new { ru.ReporterId, ru.ReporteeId});

modelBuilder.Entity<UserReports>()
   .HasOne(ru => ru.Reportee)
   .WithMany(u => u.Reporters)
   .OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<UserReports>()
   .HasOne(ru => ru.Reporter)
   .WithMany(u => u.Reportees)
   .OnDelete(DeleteBehavior.Restrict);
User.cs

public class UserReports
{
    public int ReporterId { get; set; }
    public User Reporter { get; set; }

    public int ReporteeId { get; set; }
    public User Reportee { get; set; }
}
public class User: IdentityUser<int>
{
    public DateTime DateOfBirth { get; set; }
    public string KnownAs { get; set; }
    public DateTime Created { get; set; }
    public DateTime LastActive { get; set; }

    public ICollection<UserReports> Reporters { get; set; } = new Collection<UserReports>();
    public ICollection<UserReports> Reportees { get; set; } = new Collection<UserReports>();
}
modelBuilder.Entity<UserReports>()
   .HasKey(ru => new { ru.ReporterId, ru.ReporteeId});

modelBuilder.Entity<UserReports>()
   .HasOne(ru => ru.Reportee)
   .WithMany(u => u.Reporters)
   .OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<UserReports>()
   .HasOne(ru => ru.Reporter)
   .WithMany(u => u.Reportees)
   .OnDelete(DeleteBehavior.Restrict);
公共类用户:IdentityUser
{
公共日期时间出生日期{get;set;}
公共字符串KnownAs{get;set;}
已创建公共日期时间{get;set;}
public DateTime LastActive{get;set;}
public ICollection Reporters{get;set;}=new Collection();
公共ICollection报告对象{get;set;}=new Collection();
}
ApplicationObjbContext.cs

public class UserReports
{
    public int ReporterId { get; set; }
    public User Reporter { get; set; }

    public int ReporteeId { get; set; }
    public User Reportee { get; set; }
}
public class User: IdentityUser<int>
{
    public DateTime DateOfBirth { get; set; }
    public string KnownAs { get; set; }
    public DateTime Created { get; set; }
    public DateTime LastActive { get; set; }

    public ICollection<UserReports> Reporters { get; set; } = new Collection<UserReports>();
    public ICollection<UserReports> Reportees { get; set; } = new Collection<UserReports>();
}
modelBuilder.Entity<UserReports>()
   .HasKey(ru => new { ru.ReporterId, ru.ReporteeId});

modelBuilder.Entity<UserReports>()
   .HasOne(ru => ru.Reportee)
   .WithMany(u => u.Reporters)
   .OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<UserReports>()
   .HasOne(ru => ru.Reporter)
   .WithMany(u => u.Reportees)
   .OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity()
.HasKey(ru=>new{ru.ReporterId,ru.ReporteeId});
modelBuilder.Entity()
.HasOne(ru=>ru.Reportee)
.有很多(u=>u.记者)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity()
.HasOne(ru=>ru.Reporter)
.有许多(u=>u.报告对象)
.OnDelete(DeleteBehavior.Restrict);
迁移

添加迁移后,EF core将生成这种类型的迁移,这将创建额外的列

ReporterId1 = table.Column<int>(nullable: true)

migrationBuilder.CreateTable(
        name: "ReportUsers",
        columns: table => new
        {
           ReporterId = table.Column<int>(nullable: false),
           ReporteeId = table.Column<int>(nullable: false),
           ReporterId1 = table.Column<int>(nullable: true),
           Message = table.Column<string>(nullable: false)
        },
ReporterId1=table.Column(可空:true)
migrationBuilder.CreateTable(
名称:“报告用户”,
列:表=>new
{
ReporterId=table.Column(可空:false),
ReporteeId=table.Column(可空:false),
ReporterId1=table.Column(可空:true),
Message=table.Column(可空:false)
},

您的配置中没有提到
HasForeignKey
,这就是为什么映射外键时出现混淆的原因,因为它是
多对多
,具有相同的实体。请按以下方式更新您的配置:

modelBuilder.Entity<UserReports>()
   .HasKey(ru => new { ru.ReporterId, ru.ReporteeId});

modelBuilder.Entity<UserReports>()
   .HasOne(ru => ru.Reportee)
   .WithMany(u => u.Reporters)
   .HasForeignKey(ru => ru.ReporteeId); // <-- Here it is
   .OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<UserReports>()
   .HasOne(ru => ru.Reporter)
   .WithMany(u => u.Reportees)
   .HasForeignKey(ru => ru.ReporterId); // <-- Here it is
   .OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity()
.HasKey(ru=>new{ru.ReporterId,ru.ReporteeId});
modelBuilder.Entity()
.HasOne(ru=>ru.Reportee)
.有很多(u=>u.记者)
.HasForeignKey(ru=>ru.ReporteeId);//ru.Reporter)
.有许多(u=>u.报告对象)

.HasForeignKey(ru=>ru.ReporterId);//@很高兴听到你的问题已经解决了。