2016-01-16 29 views
1

我有兩個表無效的列名TableName_ID錯誤

  1. PropertyListing - 它存儲的性能用戶的詳細信息添加,用FK
  2. PropertyAvailability - 這是存儲性能狀態(現在可用的表後, 3個月,...)

我試圖執行與這兩個表這樣

public partial class PropertyListing 
{ 
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int ID { get; set; } 
    public string StreetAddress { get; set; } 
    //the column that links with PropertyAvaibility table PK 
    public byte? Availability { get; set; } 

    public bool Status { get; set; } 

    public virtual PropertyAvailability PropertyAvailability { get; set; } 
} 

public partial class PropertyAvailability 
{ 
    public byte ID { get; set; } 
    public string Status { get; set; } 

    public virtual ICollection<PropertyListing> PropertyListings { get; set; } 
    public PropertyAvailability() 
    { 
     PropertyListings = new List<PropertyListing>(); 
    } 
} 
一個一對多的關係(流利API)

我對OnModelCreating

modelBuilder.Entity<PropertyListing>() 
     .HasRequired(pl => pl.PropertyAvailability) 
     .WithMany(pa => pa.PropertyListings) 
     .HasForeignKey(pl => pl.Availability); 

它失敗,此錯誤調用此,

無效的列名稱PropertyListing_ID「。

教程我用:http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

出了什麼問題?我知道我搞砸了EF6期待的命名約定,但是沒有解決方法嗎?

P.S:在我們的SO中,我看到ef3左右的這個問題,但我無法找到任何解決方案,因此也沒有問題。

回答

1

列屬性添加到您的類

public partial class PropertyListing 
{ 
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Column("ID")] 
    public int ID { get; set; } 
    public string StreetAddress { get; set; } 
    //the column that links with PropertyAvaibility table PK 
    public byte? Availability { get; set; } 

    public bool Status { get; set; } 

    public virtual PropertyAvailability PropertyAvailability { get; set; } 
} 
+0

我認爲列屬性用來如果屬性名稱是不同的?這是如何解決這個問題的?只是好奇。 –

+0

很有意思。你爲什麼這麼認爲?它沒有工作 – naveen