如何使用实体框架从mysql获取最后一个插入的id

时间:2016-08-15 13:12:52

标签: c# mysql entity-framework

var forSentTable = new mailsent { Subject = Subject.Text, Mailbody = editor1.InnerText, SentTime = DateTime.Now, SentBy = mailFromEmail };

// var forSentTableBody = new MailSent { };
context.mailsents.Add(forSentTable);

//context.MailSents.Add(forSentTableBody);
context.SaveChanges();

//tried this one but not working with mysql(but it works with sql server)
var mailID = forSentTable.MailID;

1 个答案:

答案 0 :(得分:1)

如果您使用数据库优先模型。您必须将Identity设置为model的StoreGeneratedPattern属性的值。  如果你使用代码第一模型。试试这个

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
 public Guid ID { get; set; }