简体   繁体   English

Linq To Sql-主键语法

[英]Linq To Sql - Primary Key syntax

I am using LINQ To SQL and creating my database using CreateDatabase method. 我正在使用LINQ To SQL,并使用CreateDatabase方法创建数据库。 I understand from MSDN that the syntax to create an auto generated primary key field is 从MSDN我了解到,创建自动生成的主键字段的语法是

[Column(Storage="_ID", IsPrimaryKey=true, AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsDbGenerated=true)]

Can someone explain me what does it mean by AutoSync.OnInsert and what is the usage of other members of AutoSync enum? 有人可以解释一下AutoSync.OnInsert是什么意思,以及AutoSync枚举的其他成员的用法是什么? .ie. .ie。 AutoSync.Always, AutoSync.Default etc. 始终自动同步,默认默认等

And what's the significance of the word 'Identity' in DbType, when I have already specified the column as primary key? 当我已经将列指定为主键时,DbType中的“ Identity”一词有什么意义?

Identity means that the ID column has an autogenerated value that will be inserted automatically on any insert. 身份表示ID列具有一个自动生成的值,该值将自动插入到任何插入中。 When you insert a row the first time the value for ID will be 1 the next time 2 and so on. 第一次插入行时,ID的值将在下一次2时为1,依此类推。

AutoSync Instructs the common language runtime (CLR) to retrieve the value after an insert or update operation. AutoSync指示公共语言运行时(CLR)在插入或更新操作后检索值。

Options: Always, Never, OnUpdate, OnInsert. 选项: 始终,从不,OnUpdate,OnInsert。

Source 资源

Never: You will never get back an ID in your entity (as long as you won't load the Entity from Database). 从不:只要您不从数据库中加载实体,就永远不会在实体中获取ID。 Always means that after every insert or update Linq2Sql operation it will retrieve the value from the Database. 始终意味着在每次插入或更新Linq2Sql操作之后,它将从数据库中检索值。

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM