EF Code First 学习笔记:约定配置
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就是Fluent API,通过新增相应的配置类来覆盖默认配置。现在我们用这两个来对比了解EF中的约定配置。
主键:KEY
Data Annotations:通过Key关键字来标识一个主键
[Key]
public int DestinationId { get; set; }
Fluent API:

public class BreakAwayContext : DbContext
{
public DbSet<Destination> Destinations { get; set; }
public DbSet<Lodging> Lodgings { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Fluent API
modelBuilder.Entity<Destination>().HasKey(d => d.DestinationId);
base.OnModelCreating(modelBuilder);
}
}

外键
Data Annotations:
public int DestinationId { get; set; }
[ForeignKey("DestinationId")]
public Destination Destination { get; set; }
注意,指定列名存在,如上面的DestinationId,则类中必须存在名称为DestinationId的属性。
Fluent API:
modelBuilder.Entity<Lodging>().HasRequired(p => p.Destination).WithMany(p=>p.Lodgings).HasForeignKey(p => p.DestinationId);
长度
Data Annotations:通过StringLength(长度),MinLength(最小长度),MaxLength(最大长度)来设置数据库中字段的长度。
[MinLength(10),MaxLength(30)]
public string Name { get; set; }
[StringLength(30)]
public string Country { get; set; }
Fluent API:没有设置最小长度这个方法。
modelBuilder.Entity<Destination>().Property(p => p.Name).HasMaxLength(30);
modelBuilder.Entity<Destination>().Property(p => p.Country).HasMaxLength(30);
非空
Data Annotations:用Required来标识,还可以设置是否可允许空字符串,显示错误消息等。
[Required]
public string Country { get; set; }
[Required(ErrorMessage="请输入描述")]
public string Description { get; set; }
Fluent API:
modelBuilder.Entity<Destination>().Property(p => p.Country).IsRequired();
数据类型
Data Annotations:TypeName
//将string映射成ntext,默认为nvarchar(max)
[Column(TypeName = "ntext")]
public string Owner { get; set; }
Fluent API:
modelBuilder.Entity<Lodging>().Property(p => p.Owner).HasColumnType("ntext");
表名
Data Annotations:Table

[Table("MyLodging")]
public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public decimal Price { get; set; }
public bool IsResort { get; set; }
public Destination Destination { get; set; }
}

Fluent API:
modelBuilder.Entity<Lodging>().ToTable("MyLodging");
列名
Data Annotations:Column
[Column("MyName")]
public string Name { get; set; }
Fluent API:
modelBuilder.Entity<Lodging>().Property(p => p.Name).HasColumnName("MyName");
自增长
如果主键是int类型,EF为默认设置为增长。但如果是GUID类型,则要显示的设置自增长。
Data Annotations:DatabaseGenerated

public class Person
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid SocialId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

看看创建数据的脚本,会加一句
ALTER TABLE [dbo].[People] ADD DEFAULT (newid()) FOR [SocialId]
Fluent API:
modelBuilder.Entity<Person>().Property(p => p.SocialId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
忽略列映射
类中有些属性,特别是一些通过计算或合并列得出的结果,我们并不需要其记录到数据库中,就可以通过配置不让它生成在数据库中。
Data Annotations:NotMapped

[NotMapped]
public string Name
{
get
{
return FirstName + " " + LastName;
}
}

Fluent API:NotMapped
modelBuilder.Entity<Person>().Ignore(p => p.Name);
忽略表映射
对于不需要映射到数据库中的表,我们也可以取消其映射。
Data Annotations:

[NotMapped]
public class Person
{
[Key]
public Guid SocialId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

Fluent API:
modelBuilder.Ignore<Person>();
时间戳
时间戳只对数据类型为byte[]的属性有效,并且一个类中只能有一个设置为时间戳的属性。
Data Annotations:Timestamp
[Timestamp]
public Byte[] TimeStamp { get; set; }
Fluent API:
modelBuilder.Entity<Lodging>().Property(p => p.TimeStamp).IsRowVersion();
复杂类型
Data Annotations:ComplexType
[ComplexType]
public class Address
{
public string Country { get; set; }
public string City { get; set; }
}
Fluent API:
modelBuilder.ComplexType<Address>();
关于什么是复杂类型,可以参见:http://www.cnblogs.com/Gyoung/archive/2013/01/17/2864747.html
EF Code First 学习笔记:约定配置的更多相关文章
- EF Code First 学习笔记:约定配置 Data Annotations+Fluent API
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...
- [转载]EF Code First 学习笔记:约定配置
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...
- 【转】EF Code First 学习笔记:约定配置
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...
- EF Code First 学习笔记:约定配置(转)
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一 ...
- EF Code First学习笔记
EF Code First学习笔记 初识Code First EF Code First 学习笔记:约定配置 Entity Framework 复杂类型 Entity Framework 数据生成选项 ...
- EF Code First教程-02 约定配置
示例: public class Phone { [Key] //主键 public int Id { get; set; } [Required] //不能为空 [MinLength(),MaxLe ...
- EF Code First学习笔记:数据库创建
控制数据库的位置 默认情况下,数据库是创建在localhost\SQLEXPRESS服务器上,并且默认的数据库名为命名空间+context类名,例如我们前面的BreakAway.BreakAwayCo ...
- EF Code First 学习笔记:表映射
多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Perso ...
- EF Code First 学习笔记:表映射 多个Entity到一张表和一个Entity到多张表
多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Per ...
随机推荐
- MySQL的复制原理及配置
MySQL 的数据库的高可用性的架构大概有以下几种:集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单介绍复制的原理及配置,以及一些常见的问题. 一.复制的原理 MySQL 复制基于主服 ...
- jquery全局加载函数的几种方式;
1.使用javascript方式(function(){})(); 2.使用jQuery(function($) {}); 3.使用$(document).ready(function(){}); 其 ...
- mybatis的javaType和ofType
都是指定对象的类型 不同的是当使用反向查询select从另一个maper文件中取出数据时必须用ofType 都可以为collection和association是指定对象的类型, 都不是必须写的, 只 ...
- C# Windows - TabControl
TabControl控件的属性 - 一般用于控制TabPages对象容器的外观,特别是显示的选项卡的外观 属性 说明 Alignment 控制选项卡在选项卡控件的什么位置显示 Appearance 控 ...
- ServiceStack.Redis
什么是Redis 首先,简述一下什么是Redis. Redis是一个开源.支持网络.基于内存.键值对存储数据库,使用ANSI C编写.从2013年5月开始,Redis的开发由Pivotal赞助.在这之 ...
- [转载]C#导入XLS数据到数据库
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- zoj 3725
题意: n个格子排成一条直线,可以选择涂成红色或蓝色,问最少 m 个连续为红色的方案数. 解题思路: 应该是这次 ZOJ 月赛最水的一题,可惜还是没想到... dp[i] 表示前 i 个最少 m 个连 ...
- spoj 297
就是对距离进行二分找最大值 .... #include <cstring> #include <cstdio> #include <algorithm> #incl ...
- fiddler插件开发step by step 1
Fiddler 是优秀的抓包工具,有着众多的优秀插件.Fiddler 软件是由C#语言开发的,运行在.net Framework 框架之上,所以我们也可以使用vs来开发自己的Fiddler插件,下面就 ...
- linux distribution是什么?
linux distribution,即Linux发行版,有很多种类,包括Fedora,Ubuntu,Debian,Red Hat,SuSE等,其内核都是差不多的,只是界面设计和功能上各有千秋.