4

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.

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.

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.

Per jwatts1980 Recommendation

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. Column: 'invoice_id'

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

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.

Community
  • 1
  • 1
99823
  • 2,407
  • 7
  • 38
  • 60
  • Have you checked in database when you insert a new record, what is the value for InvoiceId? – serhads Mar 20 '14 at 15:57
  • Yes, the InvoiceId is the auto incremented int ID - the last one created was 90761 – 99823 Mar 20 '14 at 15:59
  • Has anybody found a solution to this as I am facing the same issue. data gets inserted but object Id is not assigned after SaveChanges(). – Code_maniac Dec 18 '18 at 11:50

1 Answers1

5

This attribute might help

[Key]
[Column("invoice_id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int InvoiceId { get; set; }
jwatts1980
  • 7,254
  • 2
  • 28
  • 44
  • Going to give this a shot right now and let you know... thanks – 99823 Mar 20 '14 at 16:02
  • I'm not sure this will change anything. Default convention on an int key field is to already be an identity. He also mentioned that the record is getting inserted correctly. Seems something else weird is going on. – Dismissile Mar 20 '14 at 16:14
  • I had a case once like this where I forgot to set the field as the primary key on the database side. – jwatts1980 Mar 20 '14 at 16:24
  • Hi jwatts - I have updated my question, your answer did not immediately solve the problem - however it did lead to the solution. I have voted up your answer because it helped bring me to the solution. thanks – 99823 Mar 20 '14 at 16:27