开发者

Linq To Sql: handle NewID()

开发者 https://www.devze.com 2023-01-18 07:14 出处:网络
hey, im trying to add data to my sqlserver by using Linq To Sql, in the table, the data design to get NEWID() when new row is inserted,开发者_运维知识库

hey, im trying to add data to my sqlserver by using Linq To Sql, in the table, the data design to get NEWID() when new row is inserted,开发者_运维知识库 but the linq to sql ignore it and the cell in null,

thanks!


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.

Linq To Sql: handle NewID()

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消