Linq To Sql:处理NewID() [英] Linq To Sql: handle NewID()

查看:136
本文介绍了Linq To Sql:处理NewID()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,即时通讯试图通过使用Linq To Sql将数据添加到我的sqlserver中, 在表中,进行数据设计以在插入新行时获取NEWID(), 但是sql的linq会忽略它,而单元格为null,

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,

谢谢!

推荐答案

听起来好像您希望插入时自动为您生成一个Guid值.听起来该列的DEFAULTNewID().

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().

在LINQ To SQL模型中,导航到表的属性,该属性的值是自动生成的.右键单击属性,并确保将属性自动生成的值设置为True.这样可以确保生成的INSERT语句不包含此表属性的值.

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

这篇关于Linq To Sql:处理NewID()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆