Linq To Sql-主键语法 [英] Linq To Sql - Primary Key syntax

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

问题描述

我正在使用LINQ To SQL,并使用CreateDatabase方法创建数据库.从MSDN我了解到,创建自动生成的主键字段的语法是

I am using LINQ To SQL and creating my database using CreateDatabase method. I understand from MSDN that the syntax to create an auto generated primary key field is

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

有人可以向我解释AutoSync.OnInsert是什么意思,以及AutoSync枚举的其他成员的用法是什么?.IE.始终自动同步,默认默认同步等.

Can someone explain me what does it mean by AutoSync.OnInsert and what is the usage of other members of AutoSync enum? .ie. AutoSync.Always, AutoSync.Default etc.

当我已经将列指定为主键时,DbType中的"Identity"一词有什么意义?

And what's the significance of the word 'Identity' in DbType, when I have already specified the column as primary key?

推荐答案

Identity表示ID列具有自动生成的值,该值将在任何插入时自动插入.第一次插入行时,ID的值将为1,下次为2,以此类推.

Identity means that the ID column has an autogenerated value that will be inserted automatically on any insert. When you insert a row the first time the value for ID will be 1 the next time 2 and so on.

自动同步指示公共语言运行库(CLR)在插入或更新操作之后检索该值.

AutoSync Instructs the common language runtime (CLR) to retrieve the value after an insert or update operation.

选项:始终,从不,OnUpdate,OnInsert.

来源

从不:只要您不从数据库中加载实体,您就永远不会取回ID.始终意味着在每次插入或更新Linq2Sql操作之后,它将从数据库中检索值.

Never: You will never get back an ID in your entity (as long as you won't load the Entity from Database). Always means that after every insert or update Linq2Sql operation it will retrieve the value from the Database.

这篇关于Linq To Sql-主键语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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