How to Handle Primary Key in Entity Framework 5 Code First

后端 未结 1 419
难免孤独
难免孤独 2020-12-06 13:43

In my EF5 code-first models, creation of new records works better if the database sets the primary key. I am using a Guid for primary key and if DatabaseGeneratedOptio

相关标签:
1条回答
  • 2020-12-06 14:12

    AddOrUpdate has an overload that allows you to specify the key

    From MSDN

    So you need to supply the method with the natural key:

    context.Record.AddOrUpdate(c => c.RecordName, new Record()
                {
                    RecordName = "New Record",
                    Created = DateTime.UtcNow,
                    Updated = DateTime.UtcNow,
                 })
    
    0 讨论(0)
提交回复
热议问题