在LINQ to SQL中自动生成 [英] auto generate in LINQ to SQL
问题描述
我有一个表,其中有2列ID和名称.我为ID列设置了是"身份.
I have a table whit 2 columns , ID and name .I set 'YES' Identity for ID column .
我想在表LINQ中插入数据.我只想从应用程序中的用户那里获取名称列,然后将ID列自动填充到数据库中,而我无法提供该列的数据并填充它.
I want to insert data to table whit LINQ .I want to get only name column from user in my application , and then ID column fill automatic to database and I can't give data that column and fill whitout I give it .
我该怎么办?
我用c#编写并使用LINQ.
I write in c# and work whit LINQ .
推荐答案
所以您的数据库字段已经是一个INT IDENTITY-对吗?
So your database field already is a INT IDENTITY - right?
接下来,在LINQ to SQL设计器中,需要确保您的列设置为:
Next, in your LINQ to SQL designer, you need to make sure your column is set to:
- 自动生成的值= TRUE
- 自动同步=插入
现在,如果您向数据库添加一个条目,则ID将从数据库分配给它的值自动更新:
Now if you add an entry to your database, the ID will be automatically updated from the value assigned to it by the database:
YourClass instance = new YourClass();
instance.Name = "some name";
YourDataContext.InsertOnSubmit(instance);
YourDataContext.SubmitChanges();
在SubmitChanges调用之后,您的instance.ID
现在应该包含数据库中的ID.
After the SubmitChanges call, your instance.ID
should now contain the ID from the database.
这篇关于在LINQ to SQL中自动生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!