[英]Db.SaveChanges Not Assigning Primary Key ID After Insert - Code First Entity Framework
I have a really weird situation with one class specifically. 我有一个非常奇怪的情况,特别是一个班级。 Upon adding the class to the
DbContext
to insert into the database and calling Db.SaveChanges
code first/ef is not assigning the primary key id back to the class. 将类添加到
DbContext
以插入数据库并调用Db.SaveChanges
代码后,首先/ ef不会将主键ID分配回类。
It's really odd, I've never encountered this before and I can't seem to find any solutions online. 这真的很奇怪,我以前从未遇到过这种情况,我似乎无法在网上找到任何解决方案。
Here is what the code looks like currently... 这是代码目前的样子......
Invoice Code First Class 发票代码头等舱
[Table("invoice")]
public class Invoice
{
[Key]
[Column("invoice_id")]
public int InvoiceId { get; set; }
[Column("invoice_credit_card_payment_id")]
public int InvoiceCreditCardPaymentId { get; set; }
[Column("batch_id")]
public int BatchId { get; set; }
[Column("customer_id")]
public int CustomerId { get; set; }
etc.....
}
Code to Insert Invoice into Database 将发票插入数据库的代码
var invoice = new Invoice()
{
BatchId = 0,
Memo = "",
PayDateTime = DateTime.Now,
QuickbooksAccountName = "",
QuickbooksId = "",
Terms = "",
etc....
};
DbContext.Current.Invoices.Add(invoice);
//Invoice record does insert successfully!
DbContext.Current.SaveChanges();
//This returns ZERO
var id = invoice.InvoiceId;
Additional Notes 补充说明
As a side note the invoice record is successfully inserted into the database, however, the ID is not assigned back to the invoice object. 作为旁注,发票记录已成功插入数据库,但是,ID不会分配回发票对象。
Another note - I have around 30 other code first classes that work just fine when doing inserts and getting ID's - it's just this one that is giving me issues for some weird reason. 另一个注意事项 - 我有大约30个其他代码的第一个类在插入和获取ID时工作得很好 - 只是这个因为一些奇怪的原因而给我带来问题。
Per jwatts1980 Recommendation 按照jwatts1980建议书
I updated the invoice class to reflect this 我更新了发票类以反映这一点
[Key]
[Column("invoice_id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int InvoiceId { get; set; }
This did not solve the problem immediately, however it did reveal a new error upon insert: 这并没有立即解决问题,但它确实在插入时显示了一个新错误:
A dependent property in a ReferentialConstraint is mapped to a store-generated column.
ReferentialConstraint中的依赖属性映射到存储生成的列。 Column: 'invoice_id'
栏目:'invoice_id'
I found a stackoverflow answer here which lead me to find some foreign key attributes I had setup on the invoice class: 我在这里找到了stackoverflow的答案,这使我找到了我在发票类上设置的一些外键属性:
[ForeignKey("InvoiceId")]
public ICollection<InvoiceOrder> InvoiceOrders { get; set; }
[ForeignKey("InvoiceId")]
public InvoiceCreditCardPayment InvoiceCreditCardPayment { get; set; }
Removing the ForeignKey
attributes above seemed to solve the problem so far. 到目前为止,删除上面的
ForeignKey
属性似乎解决了这个问题。 I can't say that the it won't cause an other errors, however so far everything appears to be working well. 我不能说它不会导致其他错误,但到目前为止一切似乎都运行良好。
This attribute might help 此属性可能有所帮助
[Key]
[Column("invoice_id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int InvoiceId { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.