Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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# 如何更改FK Id字段的列名?_C#_.net Core_Entity Framework Core - Fatal编程技术网

C# 如何更改FK Id字段的列名?

C# 如何更改FK Id字段的列名?,c#,.net-core,entity-framework-core,C#,.net Core,Entity Framework Core,在我的模型中,同一个表有2个多对多关系。像这样: public class BpsModel { [Key] public int Id { get; set; } public string Name { get; set; } public List<Person> Reviewers { get; set; } public List<Person> Modelers { get; set; } } public class

在我的模型中,同一个表有2个多对多关系。像这样:

public class BpsModel {
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Person> Reviewers { get; set; }
    public List<Person> Modelers { get; set; }
}

public class Person
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
公共类BpsModel{
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表审阅者{get;set;}
公共列表建模器{get;set;}
}
公共阶层人士
{
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串电子邮件{get;set;}
}
运行迁移时,数据库Person表的结构如下:

migrationBuilder.CreateTable(
    name: "Persons",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("Sqlite:Autoincrement", true),
        BpsModelId = table.Column<int>(nullable: true),
        BpsModelId1 = table.Column<int>(nullable: true),
        Email = table.Column<string>(nullable: true),
        Name = table.Column<string>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Persons", x => x.Id);
        table.ForeignKey(
            name: "FK_Persons_BpsRecords_BpsModelId",
            column: x => x.BpsModelId,
            principalTable: "BpsRecords",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
        table.ForeignKey(
            name: "FK_Persons_BpsRecords_BpsModelId1",
            column: x => x.BpsModelId1,
            principalTable: "BpsRecords",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
    });
migrationBuilder.CreateTable(
姓名:“人”,
列:表=>new
{
Id=table.Column(可空:false)
.Annotation(“Sqlite:Autoincrement”,true),
BpsModelId=table.Column(可空:true),
BpsModelId1=table.Column(可空:true),
Email=table.Column(可空:true),
Name=table.Column(可空:true)
},
约束:表=>
{
表.PrimaryKey(“PK_Persons”,x=>x.Id);
表1.外键(
名称:“FK_Persons_BpsRecords_BpsModelId”,
列:x=>x.BpsModelId,
原则性:“BpsRecords”,
主栏:“Id”,
onDelete:referentialiction.Restrict);
表1.外键(
名称:“FK_人员_BpsRecords_BpsModelId1”,
列:x=>x.BpsModelId1,
原则性:“BpsRecords”,
主栏:“Id”,
onDelete:referentialiction.Restrict);
});
如您所见,Person表有两列名称非常相似:
BpsModelId
BpsModelId1
。这是没有帮助的,因为我现在必须去挖掘哪一个是建模者,哪一个是评审者。是否有办法将列名更改为类似于
ReviewerBpsModelId
ModelerBpsModelId


谢谢

如果希望[键]在数据库中具有特定名称,可以使用模型类中的DataAnnotations来配置SQL Server列名。这叫做

所以你可以做:

public class BpsModel {
    [Key]
    [Column("my_custom_column_name")]
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Person> Reviewers { get; set; }
    public List<Person> Modelers { get; set; }
}
公共类BpsModel{
[关键]
[列(“我的自定义列名称”)]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表审阅者{get;set;}
公共列表建模器{get;set;}
}

在您的
OnModelCreating
方法中,您应该使用如下所示的流畅配置进行设置:

modelBuilder.Entity<BpsModel>()
            .HasMany(p => p.Reviewers)
            .WithOne()
            .HasForeignKey("BpsModelReviewerId"); // <- You customize the foreign key name for Reviewers association

modelBuilder.Entity<BpsModel>()
            .HasMany(p => p.Modelers)
            .WithOne()
            .HasForeignKey("BpsModelModelerId"); // <- You customize the foreign key name for Modelers association
modelBuilder.Entity()
.HasMany(p=>p.Reviewers)
.WithOne()
.HasForeignKey(“BpsModelReviewerId”);//p、 建模者)
.WithOne()

.HasForeignKey(“BpsModelModelerId”);// 这是因为您需要两个Int字段,一个用于存储Modeler代码,另一个用于审阅者,问题是这两个键被同一个表引用。因此,我们必须解决这个问题

审阅者的外键

[ForeignKey("ReviewerId")]
public int ReviewerId { get; set; }
public Person Reviewers { get; set; }
Modeler的外键

[ForeignKey("ModelerId")]
public int ModelerId { get; set; }
public Person Modelers { get; set; }
你的班级应该是这样的

public class BpsModel {
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    [ForeignKey("ReviewerId")]
    public int ReviewerId { get; set; }
    public Person Reviewers { get; set; }

    [ForeignKey("ModelerId")]
    public int ModelerId { get; set; }
    public Person Modelers { get; set; }
}

public class Person
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }

   public ICollection<BpsModel> BpsModel{ get; set; }
}

运行添加迁移,这在我的项目中很有用

这有用吗?->HasColumnName()可能是您要查找的内容这是一个选项,但他不需要使用DataAnnotations吗?@JohnOsborne如果您使用属性,您将被限制使用在Person实体中定义的属性的名称。实际代码没有为此定义外键属性。因此,不能通过数据注释自定义外键列名称。
using System.ComponentModel.DataAnnotations.Schema;