实体框架代码第一个MySQL链接表

时间:2018-01-18 12:57:32

标签: c# entity-framework ef-code-first

我有一个Property表,我有一个Property Ratios表。

属性可能有也可能没有比率数据。

我正在尝试在2个表之间创建链接,但在尝试执行“添加迁移”时遇到错误。

这些是我的表格:

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

}

这是我流畅的API配置:

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

我收到错误:

  

在模型生成期间检测到一个或多个验证错误:   pid:名称:类型中的每个属性名称必须是唯一的。已定义属性名称“pid”。

我希望通过“pid”链接表格。

谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:0)

public string pid { get; set; }移除PropertyForSale 重命名参数只是为了使其更具可读性。

AddMigration将为您执行此操作

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

}

这会在pid中创建PropertyForSale外键。

相关问题