简体   繁体   English

尝试将对象与EntityFramework关联时出现无效的列名错误

[英]Invalid Column Name Error When Trying to Associate Objects with EntityFramework

The entity framework is giving me some trouble. 实体框架给我带来了一些麻烦。

Here are my tables: 这是我的桌子:

Products 产品展示

产品表

Related Products 相关产品

相关产品表

And my DB context (part of it, anyway)... 还有我的数据库上下文(无论如何一部分)...

ApplicationContext.cs ApplicationContext.cs

public class ApplicationContext : DbContext
{
        public ApplicationContext() : base("DefaultConnection")
        {
        }

        public DbSet<Product> Products { get; set; }
        public DbSet<RelatedProduct> RelatedProducts { get; set; }
}

And the models... 还有模特...

Product.cs 产品.cs

public class Product
{
    public int ID              { get; set; }
    public string Manufacturer { get; set; }
    public string Model        { get; set; }
    public string PartNumber   { get; set; }
    public int CategoryID      { get; set; }
    public string Description  { get; set; }
    public decimal Price { get; set; }

    public virtual ICollection<RelatedProduct> RelatedProducts { get; set; }
}

RelatedProduct.cs RelatedProduct.cs

public class RelatedProduct
{
    public int ID        { get; set; }
    public int OwnerID   { get; set; }
    public int ProductID { get; set; }

    public virtual Product Owner { get; set; }
    public virtual Product Product { get; set; }
}

What I am trying to do 我要做什么

Loop through a list of related products like this: 循环浏览相关产品列表,如下所示:

<ul class="activity">
      @foreach (var related in @Model.RelatedProducts)
      {
             <li>
                  <i class="fa fa-chevron-right red"></i> <strong>@related.Product.Manufacturer:</strong> @related.Product.Model
             </li>
      }
</ul>

The Problem 问题

I keep getting this errorL 我不断收到这个错误

{"Invalid column name 'Product_ID'.\r\nInvalid column name 'Product_ID'.\r\nInvalid column name 'Product_ID'."}

Any ideas? 有任何想法吗?

You must tell EF if Product.RelatedProducts is the inverse navigation property of RelatedProduct.Owner or of RelatedProduct.Product . 您必须告诉EF Product.RelatedProductsRelatedProduct.Owner还是RelatedProduct.Product的反向导航属性。 Both would be possible and valid and EF can't decide it on its own. 两者都是可能且有效的,EF无法自行决定。

Solution with data annotations (assuming Owner is the inverse of RelatedProducts ): 带有数据注释的解决方案(假设OwnerRelatedProducts的反RelatedProducts ):

[InverseProperty("RelatedProducts")]
public virtual Product Owner { get; set; }

public virtual Product Product { get; set; }

Or with Fluent API: 或使用Fluent API:

modelBuilder.Entity<RelatedProduct>()
    .HasRequired(r => r.Owner)
    .WithMany(p => p.RelatedProducts)
    .HasForeignKey(r => r.OwnerID);

modelBuilder.Entity<RelatedProduct>()
    .HasRequired(r => r.Product)
    .WithMany()
    .HasForeignKey(r => r.ProductID)
    .WillCascadeOnDelete(false);

Probably only the Fluent API solution will work because you also need to disable cascading delete for one of the relationships which isn't possible with data annotations. 可能只有Fluent API解决方案会起作用,因为您还需要为其中一种关系禁用级联删除,这对于数据注释是不可能的。

Actually, the problem here was Entity Framework's "conventions over configuration". 实际上,这里的问题是实体框架的“配置约定”。 The convention for a foreign key column, when it is not explicitly added as a property, is to name it [RelatedProperty]_[RelatedClassPK] . 未将外键列显式添加为属性时,将其命名为[RelatedProperty]_[RelatedClassPK] In your situation that would be Owner_ID and Product_ID . 在您的情况下,这将是Owner_IDProduct_ID However, these columns don't exist on your tables, so you get an error. 但是,这些列在您的表上不存在,因此会出现错误。 The usual fix is to just tell EF explicitly what your foreign key properties are: 通常的解决方法是只告诉EF您的外键属性是什么:

[ForeignKey("Owner")]
public int OwnerID { get; set; }

public virtual Product Owner { get; set; }

With that, Entity Framework looks for an OwnerID column, instead, and all is good. 这样,Entity Framework就会查找OwnerID列,这一切都很好。 @Slauma inadvertently solved the underlying problem with the Fluent API approach, which explicitly declares the properties to use for the foreign keys with HasForeignKey . @Slauma使用Fluent API方法无意中解决了潜在的问题,该方法使用HasForeignKey显式声明了要用于外键的HasForeignKey This is an equally valid approach, but I felt you should know what was actually your problem. 这是同样有效的方法,但是我认为您应该知道实际上是您的问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EntityFramework 无效的列名 - EntityFramework Invalid Column Name 使用EntityFramework.Extended执行更新时出现“无效的列名称”错误 - “Invalid column name” error performing update with EntityFramework.Extended 尝试将字母数字值插入数据库时​​,列名无效 - Invalid column name when trying to insert alphanumeric value into database 尝试使用DbContext将实体添加到数据库时,列名无效 - Invalid column name when trying to add an entity to a database using DbContext 尝试使用SQL将数据插入数据库时​​出现“无效的列名” - “Invalid column name” when trying to insert data into database using SQL EntityFramework聚合列名称 - EntityFramework Aggregate column name 无效列执行带有实体框架的linq - Invalid column executing a linq with entityframework 从视图访问导航属性时,无效的列名错误 - Invalid Column Name Error When Accessing Navigation Properties From View 添加声明时出现错误:“无效的列名 &#39;ApplicationUser_Id&#39;” - Error: “Invalid column name 'ApplicationUser_Id'” when adding claims 从SQL数据库检索数据时出现“无效的列名”错误? - “invalid column name” error when retrieving data from SQL database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM