要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就是Fluent API,通过新增相应的配置类来覆盖默认配置。现在我们用这两个来对比了解EF中的约定配置。

主键:KEY

Data Annotations:通过Key关键字来标识一个主键

  1. [Key]
  2. public int DestinationId { get; set; }

Fluent API:

  1. public class BreakAwayContext : DbContext
  2. {
  3. public DbSet<Destination> Destinations { get; set; }
  4. public DbSet<Lodging> Lodgings { get; set; }
  5.  
  6. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  7. {
  8. //Fluent API
  9. modelBuilder.Entity<Destination>().HasKey(d => d.DestinationId);
  10. base.OnModelCreating(modelBuilder);
  11. }
  12. }

外键

Data Annotations:

  1. public int DestinationId { get; set; }
  2. [ForeignKey("DestinationId")]
  3. public Destination Destination { get; set; }

注意,指定列名存在,如上面的DestinationId,则类中必须存在名称为DestinationId的属性。

Fluent API:

  1. modelBuilder.Entity<Lodging>().HasRequired(p => p.Destination).WithMany(p=>p.Lodgings).HasForeignKey(p => p.DestinationId);

长度

Data Annotations:通过StringLength(长度),MinLength(最小长度),MaxLength(最大长度)来设置数据库中字段的长度。

  1. [MinLength(10),MaxLength(30)]
  2. public string Name { get; set; }
  3. [StringLength(30)]
  4. public string Country { get; set; }

Fluent API:没有设置最小长度这个方法。

  1. modelBuilder.Entity<Destination>().Property(p => p.Name).HasMaxLength(30);
  2. modelBuilder.Entity<Destination>().Property(p => p.Country).HasMaxLength(30);

非空

Data Annotations:用Required来标识,还可以设置是否可允许空字符串,显示错误消息等。

  1. [Required]
  2. public string Country { get; set; }
  3. [Required(ErrorMessage="请输入描述")]
  4. public string Description { get; set; }

Fluent API:

  1. modelBuilder.Entity<Destination>().Property(p => p.Country).IsRequired();

数据类型

Data Annotations:TypeName

  1. //将string映射成ntext,默认为nvarchar(max)
  2. [Column(TypeName = "ntext")]
  3. public string Owner { get; set; }

Fluent API:

  1. modelBuilder.Entity<Lodging>().Property(p => p.Owner).HasColumnType("ntext");

表名

Data Annotations:Table

  1. [Table("MyLodging")]
  2. public class Lodging
  3. {
  4. public int LodgingId { get; set; }
  5. public string Name { get; set; }
  6. public string Owner { get; set; }
  7. public decimal Price { get; set; }
  8. public bool IsResort { get; set; }
  9. public Destination Destination { get; set; }
  10.  
  11. }

Fluent API:

  1. modelBuilder.Entity<Lodging>().ToTable("MyLodging");

列名

Data Annotations:Column

  1. [Column("MyName")]
  2. public string Name { get; set; }

Fluent API:

  1. modelBuilder.Entity<Lodging>().Property(p => p.Name).HasColumnName("MyName");

自增长

如果主键是int类型,EF为默认设置为增长。但如果是GUID类型,则要显示的设置自增长。

Data Annotations:DatabaseGenerated

  1. public class Person
  2. {
  3. [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  4. public Guid SocialId { get; set; }
  5. public string FirstName { get; set; }
  6. public string LastName { get; set; }
  7. }

看看创建数据的脚本,会加一句

  1. ALTER TABLE [dbo].[People] ADD DEFAULT (newid()) FOR [SocialId]

Fluent API:

  1. modelBuilder.Entity<Person>().Property(p => p.SocialId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

忽略列映射

类中有些属性,特别是一些通过计算或合并列得出的结果,我们并不需要其记录到数据库中,就可以通过配置不让它生成在数据库中。

Data Annotations:NotMapped

  1. [NotMapped]
  2. public string Name
  3. {
  4. get
  5. {
  6. return FirstName + " " + LastName;
  7. }
  8. }

Fluent API:NotMapped

  1. modelBuilder.Entity<Person>().Ignore(p => p.Name);

忽略表映射

对于不需要映射到数据库中的表,我们也可以取消其映射。

Data Annotations:

  1. [NotMapped]
  2. public class Person
  3. {
  4. [Key]
  5. public Guid SocialId { get; set; }
  6. public string FirstName { get; set; }
  7. public string LastName { get; set; }
  8. }

Fluent API:

  1. modelBuilder.Ignore<Person>();

时间戳

时间戳只对数据类型为byte[]的属性有效,并且一个类中只能有一个设置为时间戳的属性。

Data Annotations:Timestamp

  1. [Timestamp]
  2. public Byte[] TimeStamp { get; set; }

Fluent API:

  1. modelBuilder.Entity<Lodging>().Property(p => p.TimeStamp).IsRowVersion();

复杂类型

Data Annotations:ComplexType

  1. [ComplexType]
  2. public class Address
  3. {
  4. public string Country { get; set; }
  5. public string City { get; set; }
  6. }

Fluent API:

  1. modelBuilder.ComplexType<Address>();

【转】EF Code First 学习笔记:约定配置的更多相关文章

  1. EF Code First 学习笔记:约定配置 Data Annotations+Fluent API

    要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...

  2. [转载]EF Code First 学习笔记:约定配置

    要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...

  3. EF Code First 学习笔记:约定配置

    要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...

  4. EF Code First 学习笔记:约定配置(转)

      要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一 ...

  5. EF Code First学习笔记

    EF Code First学习笔记 初识Code First EF Code First 学习笔记:约定配置 Entity Framework 复杂类型 Entity Framework 数据生成选项 ...

  6. EF Code First教程-02 约定配置

    示例: public class Phone { [Key] //主键 public int Id { get; set; } [Required] //不能为空 [MinLength(),MaxLe ...

  7. EF Code First学习笔记:数据库创建

    控制数据库的位置 默认情况下,数据库是创建在localhost\SQLEXPRESS服务器上,并且默认的数据库名为命名空间+context类名,例如我们前面的BreakAway.BreakAwayCo ...

  8. EF Code First 学习笔记:表映射

    多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Perso ...

  9. EF Code First 学习笔记:表映射 多个Entity到一张表和一个Entity到多张表

      多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Per ...

随机推荐

  1. Vue项目初始化

    1. 生成项目模板 vue init <模板名> 本地文件夹名称2. 进入到生成目录里面 cd xxx npm install3. npm run dev vue项目模板介绍: simpl ...

  2. IE8的input兼容性问题

    在chrome.firefox.IE9+都是支持input事件 在IE8中,单纯的input事件无法监听输入框中变化,需要与propertychange共用 测试代码如下: <!DOCTYPE ...

  3. 2017第八届蓝桥杯C/C++ B组省赛-等差素数列

    标题:等差素数列 2,3,5,7,11,13,....是素数序列. 类似:7,37,67,97,127,157 这样完全由素数组成的等差数列,叫等差素数数列. 上边的数列公差为30,长度为6. 200 ...

  4. menson 使用方法

    参考:http://mesonbuild.com/Running-Meson.html#configuring-the-source https://github.com/google/googlet ...

  5. builtroot 添加git 下载方式

    1.buildroot/Config.in 配置default git server eg:config xxxx_GIT_SITE string "git site" defau ...

  6. WordPress无插件实现SMTP给评论用户发送邮件提醒

    wordpress中集成PHPMalier给评论用户发送邮件提醒 首先你得去下载PHPMalier.  注:PHPMailer需PHP的socket扩展支持.如果PHPMailer连接邮箱需要ssl加 ...

  7. [LeetCode&Python] Problem 830. Positions of Large Groups

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  8. POJ2689 Prime Distance(数论:素数筛选模板)

    题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...

  9. tomcat配置https–采用JDK自带的keytool工具生成证书

    转自:http://blog.csdn.net/huangxinyu_it/article/details/41693633 有关http与https的区别请看<浅谈http与https的区别( ...

  10. 51Nod 1240:莫比乌斯函数

    1240 莫比乌斯函数  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使 ...