EF Mappings1 Entity Mappings using Fluent API
来源:互联网 发布:跳跃网络账号注册 编辑:程序博客网 时间:2024/06/01 07:28
Here, we will learn how to configure an entity using Fluent API.
We will use the following Student and Standard domain classes of the school application.
public class Student{ public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } public DateTime? DateOfBirth { get; set; } public byte[] Photo { get; set; } public decimal Height { get; set; } public float Weight { get; set; } public Standard Standard { get; set; }} public class Standard{ public Standard() { } public int StandardId { get; set; } public string StandardName { get; set; } public ICollection<Student> Students { get; set; } }
Configure Default Schema:
First, let's configure a default schema for the tables in the database. However, you can change the schema while creating the individual tables. The following example sets the default Admin schema.
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) { //Configure default schema modelBuilder.HasDefaultSchema("Admin"); }}
Map Entity to Table:
Code-First will create the database tables with the name of DbSet properties in the context class - Students and Standards in this case. You can override this convention and can give a different table name than the DbSet properties, 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) { //Configure default schema modelBuilder.HasDefaultSchema("Admin"); //Map entity to table modelBuilder.Entity<Student>().ToTable("StudentInfo"); modelBuilder.Entity<Standard>().ToTable("StandardInfo","dbo"); } }}
As you can see in the above example, we start with the Entity<TEntity>() method. Most of the time, you have tostart with the Entity<TEntity>() method to configure it using Fluent API. We have used ToTable() method to map Student entity to StudentInfo and Standard entity to StandardInfo table. Notice that StudentInfo is in Admin schema and StandardInfo table is in dbo schema because we have specified dbo schema for StandardInfo table.
Map Entity to Multiple Table:
The following example shows how to map Student entity to multiple tables in the database.
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) { modelBuilder.Entity<Student>().Map(m => { m.Properties(p => new { p.StudentId, p.StudentName}); m.ToTable("StudentInfo"); }).Map(m => { m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth}); m.ToTable("StudentInfoDetail"); }); modelBuilder.Entity<Standard>().ToTable("StandardInfo"); } }}
As you can see in the above example, we mapped some properties of Student entity to StudentInfo table and other properties to StudentInfoDetail table using Map() method. Thus, Student entity will split into two tables, as shown below.
Map method need the delegate method as a parameter. You can pass Action delegate or lambda expression in Map method, as shown below.
using System.Data.Entity.ModelConfiguration.Configuration;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) { modelBuilder.Entity<Student>().Map(delegate(EntityMappingConfiguration<Student> studentConfig) { studentConfig.Properties(p => new { p.StudentId, p.StudentName }); studentConfig.ToTable("StudentInfo"); }); Action<EntityMappingConfiguration<Student>> studentMapping = m => { m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth }); m.ToTable("StudentInfoDetail"); }; modelBuilder.Entity<Student>().Map(studentMapping); modelBuilder.Entity<Standard>().ToTable("StandardInfo"); } }}
原文链接:http://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.aspx
- EF Mappings1 Entity Mappings using Fluent API
- EF Mapping2 Entity Framework Fluent API
- EF Mapping Entity Framework Fluent API
- EF Mapping Entity Framework Fluent API
- Entity Framework Fluent API
- EF框架 Code First Fluent API
- EF CodeFirst 约定、注释及Fluent API
- Entity Framework Fluent API and Indexing
- EF power tool使用数据注释而不是 Fluent API
- ORM系列之二:EF(3) 约定、注释、Fluent API
- Entity Framework:使用Fluent API配置数据表映射
- Entity Framework Code First (五)Fluent API - 配置关系
- Using Data Table Mappings
- EF的默认映射 Data Annotations和Fluent API 配置数据库的映射
- EF Code First 学习笔记:约定配置Data Annotations+Fluent API
- Repository模式--采用EF Fluent API使用EntityTypeConfiguration分文件配置Model映射关系
- Repository模式--采用EF Fluent API使用EntityTypeConfiguration分文件配置Model映射关系
- EF Code First 使用反射加载程序集中的Fluent API配置文件
- Opencv 连通域分析
- 优维科技:DevOps落地经验十四则(上)
- Python的os.path常见用法
- E/dalvikvm: Could not find class 'cn.jpush.android.service.PushJobService', referenced from method c
- STM8S_004_UART基本收发数据
- EF Mappings1 Entity Mappings using Fluent API
- Maven的pom.xml文件的tag详解
- 51 nod 1082 与7无关的数
- java判断一个数是否是素数
- Spring整合JMS(二)——三种消息监听器
- zabbix监控nginx、php参考
- XMPP配置的一些坑
- android之MVP开发模式初探
- Android Studio出现Failed to open zip file问题的解决方法