Ef to MySql创建一个额外的列

时间:2014-07-17 11:26:52

标签: mysql database entity-framework database-schema

我正在使用代码第一种方法创建一个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?

1 个答案:

答案 0 :(得分:1)

您有重复Client_ID,因为第一个Client_ID不是外键,它只是一个普通列。 第二个Client_ID1是因为EF约定找到了 RNC上的public ICollection<RNC> RNCs { get; set; }, 但未能找到引用,在本例中为public Client Client { get;set; }

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

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