繁体   English   中英

Add-Migration一列名称被多次列出

[英]Add-Migration one column name is listed more than once

我创建了所有模型,然后使用Add-Migration Initial添加了新的迁移。

添加迁移会在迁移文件夹中创建一个预期的迁移文件,但是由于某种原因,它两次创建了一个名为client_id的列。 我希望创建一个名为client_id的列,该列具有整数类型并且不能为空。

我应该注意, client_id列引用了带有关系的Clients模型。

这是我的模型类的样子

[Table("scripter_campaigns")]
public class Campaign
{
    [Key]
    [Column(Order = 1)]
    public int id { get; set; }

    [Column(Order = 2)] 
    [StringLength(200)]
    [Required]
    [MinLength(5, ErrorMessage = "The title must be greater than 5 characters  in length"), 
     MaxLength(200)] 
    public string name { get; set; }

     [Column(Order = 3)] 
    [StringLength(200)]
    public string layout { get; set; }

     [Column(Order = 4)] 
    [StringLength(200)] 
    public string call_list_server_name { get; set; }

     [Column(Order = 5)] 
    [StringLength(200)] 
    public string call_list_table_name { get; set; }

    [StringLength(200)] 
    public string status { get; set; }


    public string intro_url { get; set; }
    public string use_transfer { get; set; }
    public string route_callback_to { get; set; }
    public string call_list_database_name { get; set; }
    public int client_id { get; set; }

    public DateTime? created_at { get; set; }
    public DateTime? modified_at { get; set; }

    public virtual Client Client { get; set; }
    //Initilize the default value

    public Campaign()
    {
        status = "Active";
        use_transfer = "No";
        route_callback_to = "Self"; 
    }

}

但它在迁移脚本中为up()方法生成了此代码

    CreateTable(
        "dbo.scripter_campaigns",
        c => new
            {
                id = c.Int(nullable: false, identity: true),
                name = c.String(nullable: false, maxLength: 200),
                layout = c.String(maxLength: 200),
                call_list_server_name = c.String(maxLength: 200),
                call_list_table_name = c.String(maxLength: 200),
                status = c.String(maxLength: 200),
                intro_url = c.String(),
                use_transfer = c.String(),
                route_callback_to = c.String(),
                call_list_database_name = c.String(),
                client_id = c.Int(nullable: false),
                created_at = c.DateTime(),
                modified_at = c.DateTime(),
                Client_id = c.Int(),
            })
        .PrimaryKey(t => t.id)
        .ForeignKey("dbo.clients", t => t.Client_id)
        .Index(t => t.Client_id);

我不知道Client_id = c.Int()的来源,但是当我尝试更新数据库时却导致了我的问题

这是错误

每个表中的列名必须唯一。 表“ scripter_campaigns”中的列名“ Client_id”被多次指定。

另外,我是否需要数据库中的外键才能在对象之间建立关系?

由于存在public virtual Client Client { get; set; } ,因此正在创建Client_id = c.Int() public virtual Client Client { get; set; } public virtual Client Client { get; set; }

这是因为您需要执行以下操作

[ForeignKey("Client")]
public int client_id { get; set; }       

当您的实体类中具有以下内容时

public virtual Client Client { get; set; }

这样[Table("scripter_campaigns")]将知道client_id的来源。

更新: [ForeignKey("Client")]正在指示public int client_id { get; set; } public int client_id { get; set; } public int client_id { get; set; }public virtual Client Client { get; set; } public virtual Client Client { get; set; } public virtual Client Client { get; set; }对象。 给出scripter_campaigns对象与Client对象有关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM