【问题标题】:Add-Migration one column name is listed more than onceAdd-Migration 一列名称被多次列出
【发布时间】:2016-02-04 19:43:38
【问题描述】:

我创建了所有模型,然后使用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() 的来源,但当我尝试更新数据库时,它给我带来了问题

这是错误

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

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

【问题讨论】:

    标签: c# asp.net-mvc entity-framework


    【解决方案1】:

    Client_id = c.Int() 被创建是因为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 virtual Client Client { get; set; } 对象的值。假设scripter_campaigns 对象与Client 对象相关。

    【讨论】:

    • 可能是我不理解关键字 virtual 的作用。 [ForeignKey("Client")] 到底是做什么的?
    • @MikeA 据我了解,virtual 允许延迟加载。 [ForeignKey("Client")] 是说 public int client_id { get; set; } 将保留来自 public virtual Client Client { get; set; } 对象的值
    • 有没有办法不使用外键?外键会增加我喜欢逃避的开销
    • @MikeA 当您在实体类中调用 public virtual Client Client { get; set; } 时,您已经在使用外键。注释只会避免重复的列。
    • 如果你不想使用外键,你可以用[ComplexType]装饰你的类。这应该只对 required:required 关系进行。 [ComplexType]导致EF将Campaign的数据写入Client同一张表,复杂类型不能为null
    猜你喜欢
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多