Junior
Junior

Reputation: 12002

Add-Migration one column name is listed more than once

I created all my model and then I added a new migration with Add-Migration Initial.

Adding migration created a migration file like expected in the migration folder, but for some reason it created a column called client_id twice. I am expecting to created a column called client_id with an integer type and not nullable.

I should note that the column client_id references the Clients model with a relation.

Here is how my model class look like

[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"; 
    }

}

but it generated this code for the up() method in the migration script

    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);

I do not understand where Client_id = c.Int() is coming from but it is causing me an issue when I try to update the database

Here is the error

Column names in each table must be unique. Column name 'Client_id' in table 'scripter_campaigns' is specified more than once.

Additionally, are Foreign keys in the database required for me to be able to build relation ship between my objects?

Upvotes: 1

Views: 877

Answers (1)

Komengem
Komengem

Reputation: 3774

Client_id = c.Int() is being created because of public virtual Client Client { get; set; }

This is because you need to do the following

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

When you have the following in your entity class

public virtual Client Client { get; set; }

This way [Table("scripter_campaigns")] will know where the client_id is coming from.

Update: [ForeignKey("Client")] is instructing public int client_id { get; set; } to hold a value from public virtual Client Client { get; set; } object. Give that scripter_campaigns object is related to Client object.

Upvotes: 6

Related Questions