列名TableName_ID无效错误

时间:2016-01-16 13:34:38

标签: c# entity-framework-6

我有两张桌子

  1. PropertyListing - 它使用FK
  2. 存储属性user add的详细信息
  3. PropertyAvailability - 它是一个存储属性状态的表(现在可用,3个月后......)
  4. 我正试图与这两个表(Fluent API)强制实现一对多的关系

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

    我在OnModelCreating

    上打电话给我
    modelBuilder.Entity<PropertyListing>()
            .HasRequired(pl => pl.PropertyAvailability)
            .WithMany(pa => pa.PropertyListings)
            .HasForeignKey(pl => pl.Availability);
    

    失败并出现此错误,

      

    列名称无效&#39; PropertyListing_ID&#39;。

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

    可能有什么不对?我知道我已经搞砸了EF6期望的命名惯例,但是还没有解决方法吗?

    P.S:我在SO中已经看到这个问题来自ef3左右,但我无法找到任何解决方案,因此无法找到问题。

1 个答案:

答案 0 :(得分:1)

将Column属性添加到您的类

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