Db.SaveChanges不指定主键ID插入后 - code首先实体框架 [英] Db.SaveChanges Not Assigning Primary Key ID After Insert - Code First Entity Framework
问题描述
我有专门一个班一个非常奇怪的情况。在添加类到的DbContext
来插入到数据库中并调用 Db.SaveChanges
code首先/ EF未分配的主密钥ID回类。
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.
这真的很奇怪,我从来没有遇到过这一点,我似乎无法在网上找到任何解决方案。
It's really odd, I've never encountered this before and I can't seem to find any solutions online.
下面是code是什么样子目前...
Here is what the code looks like currently...
发票code头等舱
[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插入到发票数据库
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;
其他注意事项
作为一个方面说明发票记录被成功地插入到数据库中,但是,ID没有被分配回发票对象。
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时工作得很好,周围的其他30 code类第一次 - 这只是这一个是给我的问题了一些奇怪的原因
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.
每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:
在ReferentialConstraint从属属性映射到一个存储生成列。专栏:INVOICE_ID
A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'invoice_id'
我发现了一个 StackOverflow的答案这里这导致我找一些国外的关键属性我有发票类设置:
I found a stackoverflow answer here which lead me to find some foreign key attributes I had setup on the invoice class:
[ForeignKey("InvoiceId")]
public ICollection<InvoiceOrder> InvoiceOrders { get; set; }
[ForeignKey("InvoiceId")]
public InvoiceCreditCardPayment InvoiceCreditCardPayment { get; set; }
删除 ForeignKey的
属性上面似乎到目前为止来解决这个问题。我不能说,它不会导致其他错误,但到目前为止,一切似乎运作良好。
Removing the ForeignKey
attributes above seemed to solve the problem so far. I can't say that the it won't cause an other errors, however so far everything appears to be working well.
推荐答案
该属性可以帮助
[Key]
[Column("invoice_id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int InvoiceId { get; set; }
这篇关于Db.SaveChanges不指定主键ID插入后 - code首先实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!