dataannotations用于配置模型类,它将突出显示最常用的配置。dataannotations也被许多.net应用程序所理解,例如asp.net mvc,它允许这些应用程序利用相同的注释来进行客户端验证。dataannotation属性重写默认的code-first约定。
system.componentmodel.dataannotations包括以下影响列的可空性或大小的属性。
system.componentmodel.dataannotations.schema命名空间包括以下影响数据库模式的属性。
实体框架(entity framework或简称为ef )依赖于具有用于跟踪实体的键值的每个实体。 code first依赖的其中一个约定是它如何暗示哪个属性是每个code first类中的键。
约定是寻找一个名为id的属性,或者将类名和id结合起来的属性,比如studentid。 该属性将映射到数据库中的主键列。学生,课程和入学课程遵循这个约定。
现在让假设student类使用名称stdntid而不是id。 当code first找不到符合此约定的属性时,它将抛出一个异常,因为entity framework要求必须具有一个键属性。
可以使用键注释来指定哪个属性将被用作entitykey。
下面来看看包含stdntid的student类。 它不遵循默认的code first约定,所以要处理这个问题,添加了key属性,使stdntid列成为主键。
public class student{ [key] public int stdntid { get; set; } public string lastname { get; set; } public string firstmidname { get; set; } public datetime enrollmentdate { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
在运行应用程序并查看sql server资源管理器中的数据库时,您将看到现在students表中的主键是:stdntid。
实体框架(entity framework)也支持复合键。 复合键是由多个属性组成的主键。例如,有一个drivinglicense类,其主键是licensenumber和issuingcountry的组合。
public class drivinglicense{ [key, column(order = 1)] public int licensenumber { get; set; } [key, column(order = 2)] public string issuingcountry { get; set; } public datetime issued { get; set; } public datetime expires { get; set; } }
当有组合键时,实体框架要求你定义键属性的顺序。可以使用column注释来指定顺序。
code first会将timestamp属性视为concurrencycheck属性,但它也将确保code first生成的数据库字段不可空。
使用rowversion或timestamp字段进行并发检查更为常见。但是,不要使用concurrencycheck注释,只要属性的类型是字节数组,就可以使用更具体的timestamp注释。在给定的类中只能有一个时间戳属性。
下面来看一个简单的例子,将timestamp属性添加到course类中。参考以下代码 -
public class course{ public int courseid { get; set; } public string title { get; set; } public int credits { get; set; } [timestamp] public byte[] tstamp { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
如上例所示,timestamp属性应用于course类的byte []属性。 所以,code first 将在courses表中创建一个时间戳列tstamp。
concurrencycheck注释允许在用户编辑或删除实体时标记一个或多个要用于数据库并发检查的属性。如果一直在使用ef designer,那么这将与将属性的“并发性模式”设置为“固定”一致。
下面来看看一个简单的例子,通过将它添加title属性到course类中来了解concurrencycheck是如何工作的。
public class course{ public int courseid { get; set; } [concurrencycheck] public string title { get; set; } public int credits { get; set; } [timestamp, datatype("timestamp")] public byte[] timestamp { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
在上面的course类中,concurrencycheck属性应用于现有的title属性。 code first将在update命令中包含title列来检查以下代码所示的乐观并发。
exec sp_executesql n'update [dbo].[courses] set [title] = @0 where (([courseid] = @1) and ([title] = @2)) ',n'@0 nvarchar(max) ,@1 int,@2 nvarchar(max) ',@0 = n'maths',@1 = 1,@2 = n'calculus' go
required注释告诉实体框架(entity framework)需要一个特定的属性。下面来看看student类,其中必需的id被添加到firstmidname属性中。required属性将强制entity framework 确保该属性中包含数据。
public class student{ [key] public int stdntid { get; set; } [required] public string lastname { get; set; } [required] public string firstmidname { get; set; } public datetime enrollmentdate { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
可以在上面的student类的示例中看到required属性应用于firstmidname和lastname。 因此,code first将在students表中创建一个not null的firstmidname和lastname列,如下图所示。
maxlength属性用于指定其他属性验证。它可以应用于类的字符串或数组类型的属性。 entity framework的 code first 将设置maxlength属性中指定的列的大小。
下面来看看maxlength(24)属性应用于title属性的以下course类。
public class course{ public int courseid { get; set; } [concurrencycheck] [maxlength(24)] public string title { get; set; } public int credits { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
当运行上述应用程序时,code-first将在coursed表中创建一个nvarchar(24)列标题,如以下屏幕截图所示。
现在当用户设置包含超过24个字符的标题时,entity framework将抛出entityvalidationerror异常。
minlength属性可指定其他属性验证,就像上面使用的maxlength属性一样。minlength属性也可以与maxlength属性一起使用,如下面的代码所示。
public class course{ public int courseid { get; set; } [concurrencycheck] [maxlength(24) , minlength(5)] public string title { get; set; } public int credits { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
如果在minlength属性中将title属性的值设置为小于指定的长度或大于maxlength属性中的指定长度,则ef将抛出entityvalidationerror异常。
stringlength还允许指定其他属性验证,如maxlength。 不同的是stringlength属性只能应用于domain类的字符串类型属性。参考以下示例代码 -
public class course{ public int courseid { get; set; } [stringlength (24)] public string title { get; set; } public int credits { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
entity framework还验证stringlength属性的属性值。 现在,如果用户设置标题(title),其中包含超过24个字符,那么ef将抛出entityvalidationerror异常。
默认代码第一个约定创建一个与类名相同的表名。 如果让code first创建数据库,则还可以更改正在创建的表的名称。可以让code first使用现有的数据库表。 但并不总是这样,有时类的名称与数据库中表的名称不能总是相匹配。
table属性重写此默认约定。 对于给定的域类,ef code first将在table属性使用指定的名称来创建一个表。
下面来看看一个类名为student的例子,按照惯例,code first假定这将映射到一个名为students的表。 如果不是这种情况,可以使用table属性指定表的名称,如以下代码所示 指定要创建的表名称为:studentinfo -
[table("studentsinfo")] public class student{ [key] public int stdntid { get; set; } [required] public string lastname { get; set; } [required] public string firstmidname { get; set; } public datetime enrollmentdate { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
现在可以看到table属性将表指定为studentsinfo。 生成表时,如下图所示的表名studentinfo。
不仅可以指定表名,还可以使用以下代码使用table属性指定表的模式。
[table("studentsinfo", schema = "admin")] public class student{ [key] public int stdntid { get; set; } [required] public string lastname { get; set; } [required] public string firstmidname { get; set; } public datetime enrollmentdate { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
在上面的例子中,表被指定为admin模式。 现在,code first将在admin模式中创建studentsinfo表,如以下屏幕截图所示。
column属性也与table属性相同,但table属性覆盖表行为,而column属性覆盖列行为。 默认代码第一个约定创建一个与属性名相同的列名。
如果让code first创建数据库,并且还希望更改表中列的名称。column属性重写此默认约定。 ef code first将在给定类属性的column属性中创建一个具有指定名称的列。
下面来看看下面的例子,其中属性名为firstmidname,按照惯例,code first假定这将映射到一个名为firstmidname的列。 如果不是要映射到firstmidname列时,可以使用column属性指定其它列的名称,如以下代码所示。
public class student{ public int id { get; set; } public string lastname { get; set; } [column("firstname")] public string firstmidname { get; set; } public datetime enrollmentdate { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
现在可以看到column属性将列指定为firstname。 生成表后,可以看到列名为firstname,如以下屏幕截图所示。
index属性是在entity framework 6.1中引入的。
注 - 如果您使用的是早期版本,则本节中的信息不适用。
可以使用index属性在一列或多列上创建索引。将属性添加到一个或多个属性将导致ef在创建数据库时在数据库中创建相应的索引。
在大多数情况下,索引使数据的检索更快,更高效。但是,使用索引重载表或视图可能会不愉快地影响其他操作(如插入或更新)的性能。
索引是实体框架中的新功能,可以通过减少从数据库查询数据所需的时间来提高code first应用程序的性能。
可以使用index属性将索引添加到数据库,并覆盖默认的“唯一”和“群集”设置,以获得最适合您的方案的索引。默认情况下,索引将被命名为ix_<属性名称>
让我们来看看以下代码,其中index属性被添加到course类credits列上。
public class cours{ public int courseid { get; set; } public string title { get; set; } [index] public int credits { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
可以看到index属性应用于credits属性。 现在,当表生成时,将在索引中看到名称为ix_credits的索引。
默认情况下,索引是非唯一的,但是可以使用isunique命名参数来指定索引应该是唯一的。 以下示例引入了一个唯一索引,如下面的代码所示。
public class course{ public int courseid { get; set; } [index(isunique = true)] public string title { get; set; } [index] public int credits { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
code first约定将处理模型中最常见的关系。 例如,通过更改student类中的key属性名称,创建了与enrollment类的关系问题。
public class enrollment{ public int enrollmentid { get; set; } public int courseid { get; set; } public int studentid { get; set; } public grade? grade { get; set; } public virtual course course { get; set; } public virtual student student { get; set; } } public class student{ [key] public int stdntid { get; set; } public string lastname { get; set; } public string firstmidname { get; set; } public datetime enrollmentdate { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
在生成数据库时,code first会在enrollment类中看到studentid属性,并按照约定将其识别为类名称加id,作为student类的外键。但是student类中没有studentid属性,而是student类中的stdntid属性。
解决方法是在enrollment中创建导航属性,并使用foreignkey dataannotation来帮助code first了解如何构建两个类之间的关系,如以下代码所示。
public class enrollment{ public int enrollmentid { get; set; } public int courseid { get; set; } public int studentid { get; set; } public grade? grade { get; set; } public virtual course course { get; set; } [foreignkey("studentid")] public virtual student student { get; set; } }
现在可以看到foreignkey属性应用于导航属性。
code first的约定在默认情况下,每个属于受支持数据类型的属性都包含getter和setter,它们在数据库中表示。 但是在应用中并不总是这样。notmapped属性将覆盖此默认约定。 例如,可能在student类中有一个属性,例如fathername,但不需要存储它到数据库表。 那么可以将notmapped属性应用于您不希望在数据库中创建列的fathername属性。 以下是代码。
public class student{ [key] public int stdntid { get; set; } public string lastname { get; set; } public string firstmidname { get; set; } public datetime enrollmentdate { get; set; } [notmapped] public int fathername { get; set; } public virtual icollection<enrollment> enrollments { get; set; } }
可以看到notmapped属性应用于fathername属性。 现在,当生成表时,将看到fathername列不会在数据库中创建,但它存在于student类中。
code first 不会为没有getter或setter的属性创建一个列。
在类之间有多个关系时使用inverseproperty。 在enrollment类中,可能想要跟踪注册“当前课程”的人员和注册“以前课程”的人员。
我们为enrollment类添加两个导航属性。
public class enrollment{ public int enrollmentid { get; set; } public int courseid { get; set; } public int studentid { get; set; } public grade? grade { get; set; } public virtual course currcourse { get; set; } public virtual course prevcourse { get; set; } public virtual student student { get; set; } }
同样,还需要添加这些属性引用course类。course类的导航属性返回到enrollment类,其中包含当前和以前的所有注册。
public class course{ public int courseid { get; set; } public string title { get; set; } [index] public int credits { get; set; } public virtual icollection<enrollment> currenrollments { get; set; } public virtual icollection<enrollment> prevenrollments { get; set; } }
如果外键属性未包含在上述类中所示的特定类中,code first会创建{class name} _ {primary key}外键列。 生成数据库表时,您将看到许多外键,如以下屏幕截图所示。
正如所看到的code first 无法自己匹配两个类的属性。 用于enrollments的数据库表应该有一个用于currcourse的外键和一个用于prevcourse的外键,但code first 将创建四个外键属性,即 -
要解决这些问题,可以使用inverseproperty注解来指定属性的对齐方式。
public class course{ public int courseid { get; set; } public string title { get; set; } [index] public int credits { get; set; } [inverseproperty("currcourse")] public virtual icollection<enrollment> currenrollments { get; set; } [inverseproperty("prevcourse")] public virtual icollection<enrollment> prevenrollments { get; set; } }
正如上面所看到的那样,当inverseproperty属性通过指定它所属的enrollment类的哪个引用属性应用于上述course类时,code first将生成数据库表,并在enrollments表中创建两个外键列,如以下屏幕截图所示。
我们建议执行上述示例以便更好地理解。