尝试将对象与EntityFramework相关联时列名错误无效 [英] Invalid Column Name Error When Trying to Associate Objects with EntityFramework

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

问题描述



这是我的表:



产品



相关产品





我的数据库上下文(其中一部分,无论如何)...



ApplicationContext.cs

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

public DbSet< Product>产品{get;组; }
public DbSet< RelatedProduct>相关产品{get;组; }
}

模型...



Product.cs

  public class Product 
{
public int ID {get;组; }
public string Manufacturer {get;组; }
public string Model {get;组; }
public string PartNumber {get;组; }
public int CategoryID {get;组; }
public string描述{get;组; }
public decimal价格{get;组; }

public virtual ICollection< RelatedProduct>相关产品{get;组; }
}

RelatedProduct.cs

  public class RelatedProduct 
{
public int ID {get;组; }
public int UserID {get;组; }
public int ProductID {get;组; }

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



我想做什么



循环遍历相关产品列表,如下所示:

 < ul class =activity> 
@foreach(var相关于@ Model.RelatedProducts)
{
< li>
< i class =fa fa-chevron-right red>< / i> <强> @ related.Product.Manufacturer:其中/强> @ related.Product.Model
< / li>
}
< / ul>



问题



我不断得到这个errorL

  {列名无效Product_ID'.\\\
Invalid column name'Product_ID'.\r\\ \\ n无效列名称'Product_ID'。}

任何想法? / p>

解决方案

您必须告诉EF,如果 Product.RelatedProducts 是反向导航属性 RelatedProduct.Owner RelatedProduct.Product 。两者都是可行且有效的,EF不能自行决定。



解决方案带有数据注释(假设所有者 RelatedProducts )的倒数:

  [InverseProperty(相关产品)] 
public virtual Product Owner {get;组; }

public virtual Product Product {get;组;

或使用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);

可能只有Fluent API解决方案可以工作,因为您还需要禁用其中一个关系的级联删除数据注释是不可能的。


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

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

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>

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. Both would be possible and valid and EF can't decide it on its own.

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

Or with 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.

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

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