简体   繁体   English

实体框架插入新实体

[英]entity framework inserting new entity

I am using the latest Entity Framework with a code-first approach. 我正在以代码优先的方式使用最新的实体框架。 I have such classes: 我有这样的课程:

[Serializable]
public class Customer
{
        [Key]
        public int Id { get; set; }
        public string CustomerInitials { get; set; }
        [InverseProperty("Customer")]
        public virtual List<Transaction> Transactions { get; set; }
}

[Serializable]
public class Transaction
{
        [Key]
        public int Id { get; set; }
        public int CustomerId { get; set; }
        [InverseProperty("Transactions")]
        public virtual Customer Customer { get; set; }
}

When I want to enter a new entry, I find the respective customer from the IDbSet<Customer> in my context and call 当我想输入一个新条目时,我从上下文中的IDbSet<Customer>中找到相应的客户,并致电

customer.Transactions.Add(newTrasnaction)
context.SaveChanges();

which gives me Invalid column name 'CustomerInitials' , which is a normal varchar column in the Customers table. 这给了我Invalid column name 'CustomerInitials' ,这是Customers表中的普通varchar列。 Loading all data works just fine, just saving that has a problem. 加载所有数据都很好,只是保存有问题。

Some additional details: When creating the new transaction, I don't set the Customer property, just CustomerId. 一些其他详细信息:创建新交易时,我没有设置Customer属性,仅设置了CustomerId。 Not sure if this is related to this problem or not. 不知道这是否与此问题有关。

Oh stupid me for not looking deeper into the exception. 哦,愚蠢的我没有深入研究异常。 It came out there was an error an insert trigger for the table. 结果表明,该表的插入触发器出错。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM