本文转自:疯狂的我  CodeFirst Fluent API

EF的好处之一就是实现了概念模型和存储模型的分离,我们可以为概念实体和存储实体起不同的名称,同时还可以将一个概念实体映射到多个存储实体,实现实体之间一对多或多对多的关系。

在CodeFirst里面为了处理这种概念实体和存储实体的对应关系,因此Fluent API就出现了。

下面就开始介绍Fluent API

先设置概念实体类

public class Blog
{
    public int Id { get; set; }
    public int BlogId { get; set; }
    public string Title { get; set; }
    public string BloggerName { get; set; }
    public string Test { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
}
 
public class Post
{
    public int Id { get; set; }
    public string Content { get; set; }
    public int FKBlogId { get; set; }
    public Blog Blog { get; set; }
    public ICollection<Comment> Comments { get; set; }
}
 
public class Comment
{
    public int Id { get; set; }
    public string Content { get; set; }
    public ICollection<Post> Posts { get; set; }
}

第一步 创建类BlogContext 派生于 DbContext 重写里面的方法 OnModelCreating

public class BlogContext : DbContext
   {
       public DbSet<Blog> Blogs { get; set; }
       public DbSet<Post> Posts { get; set; }
       public DbSet<Comment> Comments { get; set; }
       public DbSet<Supplier> Suppliers { get; set; }
        
       public class BlogContextInitializer : DropCreateDatabaseIfModelChanges<BlogContext>
       {
           protected override void Seed(BlogContext context)
           {
               base.Seed(context);
           }
       }
 
       protected override void  OnModelCreating(DbModelBuilder modelBuilder)
       {         
           base.OnModelCreating(modelBuilder);
       }       
   }

  第二步 实现概念模型和存储模型之间的映射

1、给表Blog设置主键

注:如果没有给实体设置主键,那么EF会默认的设置ID或者[类名+ID]的列为主键,如果两个都存在,EF则会以ID为主键

modelBuilder.Entity<Blog>().HasKey(c=>c.Id);

2、概念实体映射到存储实体的实体名改变

modelBuilder.Entity<Blog>().ToTable("Blog","Blogs");

3、给存储模型设置别名,EF的强大之处在于概念模型和存储模型对应的字段的名称可以不一样,但它还能认识,我估计是以键对值的方式在存储别名

modelBuilder.Entity<Blog>().Property(t => t.Test).HasColumnName("TestName");

4、设置实体之间一对多的关系 同时指定了Blog的外键:FK_BlogId

modelBuilder.Entity<Post>().HasRequired(p => p.Blog)
    .WithMany(b => b.Posts)
    .HasForeignKey(p => p.FK_BlogId);

5、设置实体之间多对多的关系 实现多对多的关系是建立一张两表对应的关系表

  

modelBuilder.Entity<Post>().HasMany(p => p.Comments)
    .WithMany(c => c.Posts).Map(mc =>
    {
        mc.ToTable("ComtentToPost");
        mc.MapRightKey("CommentId");
        mc.MapLeftKey("PostId");
    });

6、设置存储实体的字段为非空类型

modelBuilder.Entity<Blog>().Property(c => c.Title).IsRequired();

7、设置字段长度

modelBuilder.Entity<Blog>().Property(c => c.Title).HasMaxLength(12);

8、设置字段类型

modelBuilder.Entity<Blog>().Property(c => c.BloggerName).HasColumnType("varchar");

这里列出了Fluent API 常用的几个。以后学习新东西的会坚持写博客,这样不仅对所学知识进行梳理,也能加强自身印象。

CodeFirst Fluent API的更多相关文章

  1. 【EF】CodeFirst Fluent API使用记录

    我们在使用EF CodeFirst 模式生成数据库的时候进行表的代码映射关系可以采用注解模式和Fluent API模式.这里就是记录一下使用Fluent API进行表关系映射的方法. 注解模式: 回顾 ...

  2. 第十八篇 .NET高级技术之Linq与EF Code-First Fluent API基础讲解

    1.FluentApi简介 在这里提供了一个fluentapi基础的DEMO然后咱们在进一步的学习,直接上干货. 第一步在数据库创建一个表:person 第二步:新建控制台程序FluentAPI 第三 ...

  3. 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...

  4. 8.3 使用Fluent API进行属性映射【Code-First系列】

    现在,我打算学习,怎么用Fluent API来配置领域类中的属性. using System; using System.Collections.Generic; using System.Linq; ...

  5. 8.Fluent API in Code-First【Code-First系列】

    在前面的章节中,我们已经看到了各种不同的数据注解特性.现在我们来学习一下Fluent API. Fluent API是另外一种配置领域类的方式,它提供了更多的配置相比数据注解特性. Mappings[ ...

  6. 第十六节: EF的CodeFirst模式通过Fluent API修改默认协定

    一. 简介 1. 优先级:Fluent API > data annotations > default conventions. 2. 所有的Fluent API配置都要在 OnMode ...

  7. 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/move-configurations-to-seperate-class-in-cod ...

  8. EntityFramework Code-First 简易教程(七)-------领域类配置之Fluent API

    Fluent API配置: 前面我们已经了解到使用DataAnotations特性来覆写Code-First默认约定,现在我们来学习Fluent API. Fluent API是另一种配置领域类的方法 ...

  9. 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 ...

随机推荐

  1. C# DateTime的ToString()方法的使用

    Console.WriteLine("ToShortDateString:" + DateTime.Now.ToShortDateString()); Console.WriteL ...

  2. window对象BOM

    BOM的和新对象是window,他表示流浪器的一个实例,作为一个Global对象,有权访问parseInt()等方法 在全局作用域声明的变量,函数都有钱访问 ; function sayName () ...

  3. Implement Insert and Delete of Tri-nay Tree

    问题 Implement insert and delete in a tri-nary tree. A tri-nary tree is much like a binary tree but wi ...

  4. DHTML【2】--HTML

    通过题目,大家已经明确知道,从这一节开始介绍DHTML中的最基础的部分HTML,对于HTML等概念上一节已经做了概述,这一节不再赘余.在学习HTML之前,先告诉大家一个好消息,HTML不难,比C++. ...

  5. js 获取mac地址

    js 获取mac地址 function MacInfo(){ var locator =new ActiveXObject ("WbemScripting.SWbemLocator" ...

  6. Python 升级

    1.到官网下载对于的版本: 2.下载之后并解压出来,编译: tar xf python.xx.xx.tar.xz sudo mkdir /usr/local/python ./configure -- ...

  7. Android学习笔记--Broadcast, BroadcastReceiver(广播)

    参考资料:http://www.cnblogs.com/playing/archive/2011/03/23/1992030.html 在 Android 中使用 Activity, Service, ...

  8. Java学习笔记--xml构造与解析之Sax的使用

    汇总:xml的构造与解析 http://www.cnblogs.com/gnivor/p/4624058.html 参考资料:http://www.iteye.com/topic/763895 利用S ...

  9. cf E. Neatness

    http://codeforces.com/contest/359/problem/E 题意:要关掉所有房间的灯,一个步骤要么开灯,要么关灯,要么向有灯的方向前进一格.输出一种关掉所有灯的方案.不能关 ...

  10. 通过JCONSOLE监控TOMCAT的JVM使用情况

    这个也是要学入一下,JVMr 虚拟机原理不可少. 参考配置URL“: http://blog.163.com/kangle0925@126/blog/static/277581982011527723 ...