[英]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; }
}
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>
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.RelatedProducts
是RelatedProduct.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
): 带有数据注释的解决方案(假设Owner
是RelatedProducts
的反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_ID
和Product_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.