使用Fluent API进行实体映射【Code-First系列】

现在,我们来学习怎么使用Fluent API来配置实体。

一。配置默认的数据表Schema

Student实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF4
{
   public class Student
    {
       public int StudentID { get; set; }

       public string StudentName { get; set; }

       public int StuaentAge { get; set; }

       public string StudentEmail { get; set; }

       public Standard Standard { get; set; }
    }
}

Standard实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF4
{
   public class Standard
    {
       public int StandardID { get; set; }

       public int StandardName { get; set; }

       public ICollection<Student> Students { get; set; }

    }
}

上下文类:

using System;
using System.Collections.Generic;
using System.Data.Entity;
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.HasDefaultSchema("hello");
           base.OnModelCreating(modelBuilder);
       }
    }
}

然后生成的数据表Schema就是我们配置的【hello】了

当然,你也可以分别对每个表,进行设置Schema。

二。把实体映射成表

using System;
using System.Collections.Generic;
using System.Data.Entity;
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)
       {
           //配置数据表的Schema
           //modelBuilder.HasDefaultSchema("hello");

           //将实体映射成表
           modelBuilder.Entity<Student>().ToTable("WahHaHa");
           modelBuilder.Entity<Standard>().ToTable("StandardInfo","xxx");
           base.OnModelCreating(modelBuilder);
       }
    }
}

然后数据库是这样的:

打开表一个一个看:

三。将一个实体,拆分成多个表。

下面的代码是将Student实体拆分成两个表。

using System;
using System.Collections.Generic;
using System.Data.Entity;
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)
       {
           //配置数据表的Schema
           //modelBuilder.HasDefaultSchema("hello");

           //将实体映射成表
           //modelBuilder.Entity<Student>().ToTable("WahHaHa");
           //modelBuilder.Entity<Standard>().ToTable("StandardInfo","xxx");

           //将一个实体映射成多个表
           modelBuilder.Entity<Student>().Map(
               m =>
               {

                   m.Properties(p => p.StudentID);
                   m.Properties(p => p.StudentName);
                   m.ToTable("StudentInfo");

               }).Map(
               m =>
               {

                   m.Properties(p => p.StudentID);
                   m.Properties(p => p.StuaentAge);
                   m.Properties(p => p.StudentEmail);
                   m.ToTable("StudentDetails");
               });

           modelBuilder.Entity<Standard>().ToTable("StandardInfo");

           base.OnModelCreating(modelBuilder);
       }
    }
}

生成额数据库是:

上面的代码中,我们将Student实体,拆分成了两个表,StudentInfo和StudentDetail。

Map method need the delegate method as a parameter. You can pass Action delegate or lambda expression in Map method, as shown below.

Map方法需要委托作为参数。你可以传递一个Action委托或者lambda表达式在这个Map方法中。

时间: 2024-08-08 05:37:38

使用Fluent API进行实体映射【Code-First系列】的相关文章

Code First 关系 Fluent API

通过实体框架 Code First,可以使用您自己的域类表示 EF 执行查询.更改跟踪和更新函数所依赖的模型.Code First 利用称为“约定先于配置”的编程模式.这意味着 Code First 将假设类遵循 EF 用于概念模型的架构约定.在这种情况下,EF 将能够找出自己工作所需的详细信息.但是,如果您的类不遵守这些约定,则可以向类中添加配置,以向 EF 提供它需要的信息. Code First 为您提供了两种方法来向类中添加这些配置.一种方法是使用名为 DataAnnotations 的

Entity Framework Code First (五)Fluent API - 配置关系

上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ID { get; set; } public string Name { get; set; } public DateTime EnrollmentDate { get; set; } // Navigation properties public virtual Address Address

【译】第24节---Fluent API - 属性映射

原文:http://www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api.aspx 本节,我们将学习如何使用Fluent API配置实体类的属性. 我们将使用我们学校app的Student和Standard域类: public class Student { public Student() { } public int StudentKey { get; set; } pu

Fluent API 配置

EF里实体关系配置的方法,有两种: Data Annotation方式配置 也可以 Fluent API 方式配置 Fluent API 配置的方法 EF里的实体关系 Fluent API 配置分为Has和With系列的方法: Optional 可选的 Required 必须的 Many 多个 [举例]: A.HasRequired(a => a.B).WithOptional(b => b.A); 这里的a=>a.B是lambda表示写法,就是找到A类里的导航属性B(习惯问题:命名a不

一步一步学EF系列【2、Fluent API的方式来处理实体与数据表之间的映射关系。】

EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是Fluent API,通过新增相应的配置类来覆盖默认配置另外.我们主要学习Fluent API,Data Annotations可以自行去学习一下. 补充一下为什么要用Fluent API 使用DataAnnotation非常简单,但对于EntityFramework中的特性,就要在实体类中引入Ent

使用Fluent API 配置/映射属性和类型

Code First约定-Fluent API配置 使用Fluent API 配置/映射属性和类型 简介 通常通过重写派生DbContext 上的OnModelCreating 方法来访问Code First Fluent API.以下示例旨在显示如何使用 Fluent API 执行各种任务,您可以将代码复制出来并进行自定义,使之适用于您的模型. 属性映射 使用Fluent API 配置/映射属性和类型 简介 通常通过重写派生DbContext 上的OnModelCreating 方法来访问Co

Entity Framework Code First (四)Fluent API - 配置属性/类型

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Fluent API.  一般来说我们访问 Flu

Code First约定-Fluent API配置

转自:http://blog.163.com/m13864039250_1/blog/static/2138652482015283397609/ 用Fluent API 配置/映射属性和类型 简介 通常通过重写派生DbContext 上的OnModelCreating 方法来访问Code First Fluent API.以下示例旨在显示如何使用 Fluent API 执行各种任务,您可以将代码复制出来并进行自定义,使之适用于您的模型. 属性映射 Property 方法用于为每个属于实体或复杂

EF框架step by step(9)—Code First Fluent API

在上一篇中,讲述了用数据特性的方式来标识实体与数据表之间的映射关系,在Code First方法中,还可以通过Fluent API的方式来处理实体与数据表之间的映射关系. 要使用Fluent API必须在构造自定义的DbContext时,重写OnModelCreating方法,在此方法体内调用Fluent API. 如下面代码所示: publicclass BlogDbContext : DbContext{ public BlogDbContext(): base("name=BlogDB200