实体框架插入新实体

时间:2012-10-23 10:23:21

标签: c# foreign-keys data-annotations entity-framework-5

我使用最新的Entity Framework,采用代码优先的方法。 我有这样的课程:

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

当我想输入新条目时,我会从IDbSet<Customer>找到相应的客户 在我的背景下并致电

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

它给了我Invalid column name 'CustomerInitials',它是Customers表中的普通varchar列。加载所有数据工作正常,只需保存有问题。

其他一些细节:创建新事务时,我不会设置Customer属性,只需设置CustomerId。不确定这是否与此问题有关。

1 个答案:

答案 0 :(得分:0)

哦,愚蠢的是我没有深入了解异常。它出现了一个错误,表的插入触发器。