8.3 使用Fluent API进行属性映射【Code-First系列】
现在,我打算学习,怎么用Fluent API来配置领域类中的属性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF4
{
public class Student
{
public int StudentKey { get; set; }
public string StudentName { get; set; }
public int StuaentAge { get; set; }
public string StudentEmail { get; set; }
public Standard Standard { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF4
{
public class Standard
{
public int StandardKey { get; set; }
public int StandardName { get; set; }
public ICollection<Student> Students { get; set; }
}
}
请注意上面的代码中,Student,和Standard实体中的标注颜色的属性字段,我没有使用类名+ID或者ID的写法。而是使用了自定义的方式,这样Code-First默认约定就不知道,他们两个是主键列了,除非手动配置。
一、配置主键和复合主键【联合主键】
可以使用EntityTypeConfiguration类里面的HasKey方法。请注意,modelbuilder.Entity<TEntity>()泛型方法,返回的是EntityTypeConfiguration对象。
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF4
{
public class DBContextClass:DbContext
{
public DBContextClass() : base("ConnectionStrings") { }
public DbSet<Student> Students { get; set; }
//public DbSet<Standard> Standards { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//配置主键:
modelBuilder.Entity<Student>().HasKey(s => s.StudentKey);
modelBuilder.Entity<Standard>().HasKey(s => s.StandardKey);
base.OnModelCreating(modelBuilder);
}
}
}

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF4
{
public class DBContextClass:DbContext
{
public DBContextClass() : base("ConnectionStrings")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBContextClass>());
}
public DbSet<Student> Students { get; set; }
//public DbSet<Standard> Standards { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//配置主键:
//modelBuilder.Entity<Student>().HasKey(s => s.StudentKey);
modelBuilder.Entity<Standard>().HasKey(s => s.StandardKey);
//配置复合主键(联合主键)
modelBuilder.Entity<Student>().HasKey(s => new { s.StudentKey, s.StudentName });
base.OnModelCreating(modelBuilder);
}
}
}
得到的数据库是:

二、配置列名,列类型、列顺序
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF4
{
public class DBContextClass:DbContext
{
public DBContextClass() : base("ConnectionStrings")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBContextClass>());
}
public DbSet<Student> Students { get; set; }
//public DbSet<Standard> Standards { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//配置主键:
//modelBuilder.Entity<Student>().HasKey(s => s.StudentKey);
modelBuilder.Entity<Standard>().HasKey(s => s.StandardKey);
//配置复合主键(联合主键)
modelBuilder.Entity<Student>().HasKey(s => new { s.StudentKey, s.StudentName });
//配置列名,列类型,列顺序
modelBuilder.Entity<Student>().Property(s => s.StuaentAge).HasColumnName().HasColumnType("int");
base.OnModelCreating(modelBuilder);
}
}
}
生成的数据库:

modelBuilder.Entity<TEntity>().Property(expression) allows you to use different methods to configure a particular property, as shown below.
modelBuilder.Entity<TEntity>().Property(expression)这个方法,允许你配置指定的属性。

三、配置列是可空还是不可空
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF4
{
public class DBContextClass:DbContext
{
public DBContextClass() : base("ConnectionStrings")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBContextClass>());
}
public DbSet<Student> Students { get; set; }
//public DbSet<Standard> Standards { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//配置主键:
//modelBuilder.Entity<Student>().HasKey(s => s.StudentKey);
modelBuilder.Entity<Standard>().HasKey(s => s.StandardKey);
//配置复合主键(联合主键)
modelBuilder.Entity<Student>().HasKey(s => new { s.StudentKey, s.StudentName });
//配置列名,列类型,列顺序
modelBuilder.Entity<Student>().Property(s => s.StuaentAge).HasColumnName().HasColumnType("int");
//可空列
modelBuilder.Entity<Standard>().Property(s => s.StandardName).IsOptional();
base.OnModelCreating(modelBuilder);
}
}
}

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF4
{
public class DBContextClass:DbContext
{
public DBContextClass() : base("ConnectionStrings")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBContextClass>());
}
public DbSet<Student> Students { get; set; }
//public DbSet<Standard> Standards { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//配置主键:
//modelBuilder.Entity<Student>().HasKey(s => s.StudentKey);
modelBuilder.Entity<Standard>().HasKey(s => s.StandardKey);
//配置复合主键(联合主键)
modelBuilder.Entity<Student>().HasKey(s => new { s.StudentKey, s.StudentName });
//配置列名,列类型,列顺序
modelBuilder.Entity<Student>().Property(s => s.StuaentAge).HasColumnName().HasColumnType("int");
//可空列
// modelBuilder.Entity<Standard>().Property(s => s.StandardName).IsOptional();
//不可空
modelBuilder.Entity<Standard>().Property(s => s.StandardName).IsRequired();
base.OnModelCreating(modelBuilder);
}
}
}

后面本来已经,翻译好了,结果,网络不稳定,没备份,,现在直接贴英文了!!!
Configure Column Size:
Code-First will set the maximum size of a data type for a column. You can override this convention, as shown below.
namespace CodeFirst_FluentAPI_Tutorials
{
public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Set StudentName column size to 50
modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.HasMaxLength(50);
//Set StudentName column size to 50 and change datatype to nchar
//IsFixedLength() change datatype from nvarchar to nchar
modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.HasMaxLength(50).IsFixedLength();
//Set size decimal(2,2)
modelBuilder.Entity<Student>()
.Property(p => p.Height)
.HasPrecision(2, 2);
}
}
}
As you can see in the above example, we used HasMaxLength method to set the size of a column. IsFixedLength method converts nvarchar to nchar type. In the same way, HasPrecision method changed the precision of the decimal column.
Configure Concurrency Column:
You can configure a property as concurrency column using ConcurrencyToken method, as shown below.
namespace CodeFirst_FluentAPI_Tutorials
{
public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Set StudentName as concurrency column
modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.IsConcurrencyToken();
}
}
}
As you can see in the above example, we set StudentName column as concurrency column so that it will be included in the where clause in update and delete commands.
You can also use IsRowVersion() method for byte[] property to make it as a concurrency column.
8.3 使用Fluent API进行属性映射【Code-First系列】的更多相关文章
- 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
原文链接:https://www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api ...
- 8.2 使用Fluent API进行实体映射【Code-First系列】
现在,我们来学习怎么使用Fluent API来配置实体. 一.配置默认的数据表Schema Student实体 using System; using System.Collections.Gener ...
- 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx EF 6 Code-Firs ...
- Entity Framework Code First (四)Fluent API - 配置属性/类型
上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...
- 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/move-configurations-to-seperate-class-in-cod ...
- code First 三 Fluent API
Entity Framework Fluent API用于配置域类以覆盖约定. 在实体框架6中,DbModelBuilder类充当Fluent API,我们可以使用它来配置许多不同的东西.它提供了比数 ...
- Entity Framework Code First属性映射约定
Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...
- Entity Framework Code First (五)Fluent API - 配置关系
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...
- Code First 关系 Fluent API
通过实体框架 Code First,可以使用您自己的域类表示 EF 执行查询.更改跟踪和更新函数所依赖的模型.Code First 利用称为“约定先于配置”的编程模式.这意味着 Code First ...
随机推荐
- python字符串的使用
之前在网上看了关于python最基础的一些教程,看着都通俗易懂,但是在写的过程中却感觉还是很生涩.关于字符串的使用还是应该多写多练!如何将“teacher_id = 123 #老师ID”转换成字典或者 ...
- NEsper Nuget包
Esper是专门进行复杂事件处理(CEP)的流处理平台,Java版本为Esper,.Net版本为NEsper.Esper & NEsper可以方便开发者快速开发部署处理大容量消息和事件的应用系 ...
- OstrichNet 简易统计信息收集工具
Ostrich 是twitter用于监控服务器性能的一个scala库,项目地址https://github.com/twitter/ostrich, 主要功能是收集.展示统计信息, 同时也提供了关闭服 ...
- ABP理论学习之MVC视图
返回总目录 本篇目录 介绍 AbpWebViewPage基类 介绍 ABP通过Abp.Web.Mvc Nuget包集成了MVC视图.因此你可以像常规那样创建MVC视图. AbpWebViewPage基 ...
- ReactJS入门(三)—— 顶层API
本文基本跟着官方文档把API都走一遍,但会有实例来解释应该怎么用,木有比我更详细的API文档咯. React.createClass 参数:CONFIG(object) 创建一个ReactClass( ...
- 走向面试之数据库基础:一、你必知必会的SQL语句练习-Part 1
本文是在Cat Qi的参考原帖的基础之上经本人一题一题练习后编辑而成,非原创,仅润色而已.另外,本文所列题目的解法并非只有一种,本文只是给出比较普通的一种而已,也希望各位园友能够自由发挥. 一.三点一 ...
- 清晰易懂TCP通信原理解析(附demo、简易TCP通信库源码、解决沾包问题等)C#版
目录 说明 TCP与UDP通信的特点 TCP中的沾包现象 自定义应用层协议 TCPLibrary通信库介绍 Demo演示 未完成功能 源码下载 说明 我前面博客中有多篇文章讲到了.NET中的网络编程, ...
- java中文乱码解决之道(三)-----编码详情:伟大的创想---Unicode编码
随着计算机的发展.普及,世界各国为了适应本国的语言和字符都会自己设计一套自己的编码风格,正是由于这种乱,导致存在很多种编码方式,以至于同一个二进制数字可能会被解释成不同的符号.为了解决这种不兼容的问题 ...
- HaProxy+Keepalived+Mycat高可用群集配置
概述 本章节主要介绍配置HaProxy+Keepalived高可用群集,Mycat的配置就不在这里做介绍,可以参考我前面写的几篇关于Mycat的文章. 部署图: 配置 HaProxy安装 181和1 ...
- CSharpGL(1)从最简单的例子开始使用CSharpGL
CSharpGL(1)从最简单的例子开始使用CSharpGL 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo ...