Linq To Sql: handle NewID()

余生长醉 提交于 2019-11-30 23:53:23

It sounds like you're expecting a guid value to be auto-generated for you right at the time of insert. It sounds like that column has a DEFAULT of NewID().

In your LINQ To SQL model, navigate to the table's property whose value is auto-generated. Right-click, Properties, and ensure the attribute Auto Generated Value is set to True. This will ensure the INSERT statement generated doesn't include a value for this table attribute.

Then you'll be able to access that new property with the default auto-gen value as such:

Customer c = new Customer{FirstName="foo"};
db.Customers.InsertOnSubmit(c);
db.SubmitChanges();
string someGuid = c.MyGuidField.ToString();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!