无效列名称错误当试图用的EntityFramework到关联对象 [英] Invalid Column Name Error When Trying to Associate Objects with EntityFramework

查看:210
本文介绍了无效列名称错误当试图用的EntityFramework到关联对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体框架是给我一些麻烦。

The entity framework is giving me some trouble.

下面是我的表:

产品

相关产品

和我的数据库环境(它的一部分,反正)...

And my DB context (part of it, anyway)...

ApplicationContext.cs

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

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

和模型......

Product.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

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>

问题

我不断收到这个errorL

The Problem

I keep getting this errorL

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

任何想法?

推荐答案

您必须告诉EF如果 Product.RelatedProducts 逆导航属性RelatedProduct.Owner RelatedProduct.Product 的。双方将有可能有效和EF不能决定它自己的。

You must tell EF if Product.RelatedProducts is the inverse navigation property of RelatedProduct.Owner or of RelatedProduct.Product. Both would be possible and valid and EF can't decide it on its own.

与数据的注解解决方案(假设所有者是逆 RelatedProducts

Solution with data annotations (assuming Owner is the inverse of RelatedProducts):

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

public virtual Product Product { get; set; }

或用流利的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);

大概只有流利的API解决方案将工作,因为你还需要禁用级联删除这是不可能的数据注解关系之一。

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.

这篇关于无效列名称错误当试图用的EntityFramework到关联对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆