EF 6 MVC 5列名无效 [英] EF 6 MVC 5 Invalid Column Name

查看:268
本文介绍了EF 6 MVC 5列名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对数据库进行逆向工程后,为了生成models / mapping / dbcontext,一切都很好,直到我实际添加了一些东西。这是我的模型。

  public partial class Vendor 
{
public Vendor()
{
this.VendorToSuppliesAndServices = new List< VendorToSuppliesAndService>();
}

public int VendorID {get;组; }
public virtual ICollection< VendorToSuppliesAndService> VendorToSuppliesAndServices {get;组; }

}

VendorToSuppliesAndService

  public partial class VendorToSuppliesAndService 
{
public int Id {get;组; }
[ForeignKey(Vendor)]
public int VendorID {get;组; }
public string OtherInfo {get;组; }
public virtual Vendor Vendor {get;组; }
}

这里是VendorToSuppliesAndServiceMap构造函数

  //主键
this.HasKey(t => t.Id);

//属性
this.Property(t => t.OtherInfo)
.HasMaxLength(1300);

// Table&列映射
this.ToTable(VendorToSuppliesAndServices);
this.Property(t => t.Id).HasColumnName(Id);
this.Property(t => t.VendorID).HasColumnName(VendorID);
this.Property(t => t.OtherInfo).HasColumnName(OtherInfo);

//关系
this.HasRequired(t => t.Vendor)
.WithMany(t => t.VendorToSuppliesAndServices)
.HasForeignKey(d = d.VendorID);

现在问题是我做了一个补充。

 供应商供应商= .... 
VendorToSuppliesAndService service = ....
vendor.VendorToSuppliesAndServices.add(service);
context.Vendors.Add(vendor);
context.SaveChanges();

这是问题,应该注意的是没有

  VendorToSuppliesAndService service = .... 
vendor.VendorToSuppliesAndServices.add(service);

问题消失了。



这是stacktrack:

  [SqlException(0x80131904))无效列名'Vendor_VendorID'。] 
System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action`1 wrapCloseInAction)+1767866


解决方案

我解决了我的问题!几乎忘了在这里更新它。我无法确切地告诉你为什么,但是 ICollection 类是问题。通过将其更改为 IList 容器,我的所有问题都消失了。现在,如果有人能解释为什么这样工作,我会很喜欢,因为这是我的一个冰雹。



再次感谢您的帮助。


After reverse engineering a Database, to generate the models/mapping/dbcontext all is well until I actually add something. Here are my models.

public partial class Vendor
{
    public Vendor()
    {
        this.VendorToSuppliesAndServices = new List<VendorToSuppliesAndService>();
    }

    public int VendorID { get; set; }
    public virtual ICollection<VendorToSuppliesAndService> VendorToSuppliesAndServices { get; set; }

}

The VendorToSuppliesAndService

    public partial class VendorToSuppliesAndService
    {
        public int Id { get; set; }
        [ForeignKey("Vendor")]
        public int VendorID { get; set; }
        public string OtherInfo { get; set; }
        public virtual Vendor Vendor { get; set; }
    }

And Here is the VendorToSuppliesAndServiceMap Constructor

        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.OtherInfo)
            .HasMaxLength(1300);

        // Table & Column Mappings
        this.ToTable("VendorToSuppliesAndServices");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.VendorID).HasColumnName("VendorID");
        this.Property(t => t.OtherInfo).HasColumnName("OtherInfo");

        // Relationships
        this.HasRequired(t => t.Vendor)
            .WithMany(t => t.VendorToSuppliesAndServices)
            .HasForeignKey(d => d.VendorID);

Now the problem is when I do an addition.

   Vendor vendor = ....
   VendorToSuppliesAndService service = ....
   vendor.VendorToSuppliesAndServices.add(service);
   context.Vendors.Add(vendor);
   context.SaveChanges();

That's the problem, it should be noted that without

   VendorToSuppliesAndService service = ....
   vendor.VendorToSuppliesAndServices.add(service);

The problem goes away.

Here's the stacktrack:

    [SqlException (0x80131904): Invalid column name 'Vendor_VendorID'.]
          System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1767866

解决方案

I solved my problem the other day! Almost forgot to update it here.I couldn't tell you exactly why, but the "ICollection" class was the issue. By changing it to a IList container all my problems melted away. Now I would love if someone could explain why this worked, as it was a hail mary on my part.

Thanks again for the help guys.

这篇关于EF 6 MVC 5列名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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