简体   繁体   中英

Entity Framework Code First MySQL Linked Table

I have a Property table and I have a Property Ratios table.

Properties may or may not have Ratio data.

I am attempting to create a link between the 2 tables but I am getting the error when trying to do an "add-migration".

These are my tables:

public class PropertyForSale
{
    [Key]
    public int Id { get; set; }

    [Index("IX_pid", IsClustered = false, IsUnique = true, Order = 1), MaxLength(15)]
    public string pid { get; set; }

    [MaxLength(25)]
    public string Status { get; set; }

    public virtual PropertyRatios PropertyRatios { get; set; }
}

public class PropertyRatios
{
    [Key, MaxLength(15)]
    public string pid { get; set; }

    public float Zest_List_Price_Diff { get; set; }

    [Index]
    public DateTime LastUpdated { get; set; }

}

And here is my fluent API configuration:

modelBuilder.Entity<PropertyForSale>()
            .HasOptional(o => o.PropertyRatios)
            .WithOptionalPrincipal()
            .Map(o => o.MapKey("pid"));

I am getting the error:

One or more validation errors were detected during model generation: pid: Name: Each property name in a type must be unique. Property name 'pid' is already defined.

I want the tables linked via the "pid".

Can anyone see what I am doing wrong?

Remove public string pid { get; set; } public string pid { get; set; } public string pid { get; set; } from PropertyForSale Renaming parameters just to make it more readable.

AddMigration will do this for you

public class PropertyForSale
{
    [Key]
    public int Id { get; set; }

    [MaxLength(25)]
    public string Status { get; set; }

    public virtual PropertyRatios PropertyRatios { get; set; }
}

public class PropertyRatios
{
    [Key, MaxLength(15)]
    public string pid { get; set; }

    public float Zest_List_Price_Diff { get; set; }

    [Index]
    public DateTime LastUpdated { get; set; }

}

This will create pid as foreign key in PropertyForSale .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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