gpt4 book ai didi

mysql - Ef 到 MySql 创建一个额外的列

转载 作者:行者123 更新时间:2023-11-29 12:50:38 26 4
gpt4 key购买 nike

我正在使用代码优先方法创建 MySql 数据库:

    public partial class Client
{
public int ID { get; set; }
public string Name { get; set; }

//relational properties
public ICollection<RNC> RNCs { get; set; }

public Client()
{
this.RNCs = new HashSet<RNC>();
}
}

public partial class RNC
{
public int ID { get; set; }
public string Name { get; set; }

//relational properties
public int Client_ID { get; set; }

public RNC()
{
this.NodeBs = new HashSet<NodeB>();
}
}

public class ClientMap : EntityTypeConfiguration<Client>
{
public ClientMap()
{
HasRequired(x => x.Name);
}
}

public class RNCMap : EntityTypeConfiguration<RNC>
{
public RNCMap()
{
HasRequired(x => x.Name);
HasRequired(x => x.Client).WithMany(x => x.RNCs).HasForeignKey(x => x.Client_ID);
}
}

查看数据库时,数据库中有 Client_ID 和 Client_ID1。为什么以及如何删除它并将其设置为 FK?

最佳答案

您有重复的Client_ID因为第一个Client_ID不是外键,它只是一个普通的列。第二个Client_ID1是因为 EF 约定发现 public ICollection<RNC> RNCs { get; set; }在共和党全国委员会,但也找不到引用,在本例中 public Client Client { get;set; }

解决方案是删除 Client_ID属性 OR(还添加导航引用 + 指定外键属性)。

public partial class RNC
{
//relational properties
[ForeignKey("Client")]
public int Client_ID { get; set; }
public Client Client { get; set; }
}

关于mysql - Ef 到 MySql 创建一个额外的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24802275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com