Entity Framework实体拆分多个表

一、概念

实体拆分:一个实体拆分成多个表,如Product实体,可以拆分成Product和ProductWebInfo两个表,Product表用于存储商品的字符类信息,ProductWebInfo用于存储商品的图片信息,两张表通过SKU进行关联。

1、Product实体类结构:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 实体拆分.Model
{
    public class Product
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)] //设置主键需要自己填充
        public int SKU { get; set; }
        public string Description { get; set; }

        public decimal Price { get; set; }

        public string ImageURL { get; set; }
    }
}

2、数据实体类结构:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 实体拆分.Model;

namespace 实体拆分.DatabaseContext
{
    public class EFDbContext :DbContext
    {
        public EFDbContext()
            : base("name=Default")
        { }

        public DbSet<Product> Products { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>().Map(p =>
            {
                p.Properties(m => new { m.SKU, m.Price, m.Description });
                p.ToTable("Product");
            })
            .Map(p =>
            {
                p.Properties(m => new { m.SKU, m.ImageURL });
                p.ToTable("ProductWebInfo");
            });

            base.OnModelCreating(modelBuilder);
        }
    }
}

3、使用数据迁移生成数据库,生成后的表结构如下图所示:

4、测试数据:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 实体拆分.DatabaseContext;

namespace 实体拆分
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new EFDbContext())
            {
                context.Products.Add(new Model.Product() {
                    SKU=293,
                    Description="C#高级编程(第10版)",
                    Price=299 ,
                    ImageURL="http://image.baidu.com/1.jpg"
                });
                // 保存
                context.SaveChanges();
            }

            Console.WriteLine("创建成功");
            Console.ReadKey();
        }
    }
}

5、运行程序,查询数据库结果

总结

将实体拆分成多表的步骤:

1、在工程中创建一个新类继承自DbContext类。
2、创建Product的POCO类。
3、在新创建的DbContext子类中添加属性:DbSet<Product>。
4、重写DbContext类的OnModelCreating()方法。

点此下载示例代码

到此这篇关于Entity Framework实体拆分多个表的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Entity Framework表拆分为多个实体

    概念 表拆分:一个表拆分成多个实体,例如Photograph表,可以拆分为Photograph和PhotographFullImage两张表. 1.Photograph实体结构: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Li

  • Entity Framework管理一对二实体关系

    在上一篇文章中,简单的介绍了使用Fluent API如何管理一对一的实体关系,在这篇文章中,接着介绍Fluent API如何管理一对多的实体关系. 要在数据库中配置一对多关系,我们可以依赖EF约定,还可以使用数据注解或Fluent API来显式创建关系.接下来使用捐赠者Donator和支付方法PayWay这两个类来举例子,这里的一对多关系是:一个人可以通过多种支付方式赞助我. 支付方式类PayWay结构如下: using System; using System.Collections.Gene

  • Entity Framework管理一对一实体关系

    我们现在已经知道如何使用Code First来定义简单的领域类,并且如何使用DbContext类来执行数据库操作.现在我们来看下数据库理论中的多样性关系,我们会使用Code First来实现下面的几种关系: 1.一对一关系: one to one 2.一对多关系: one to many 3.多对多关系::many to many 首先要明确关系的概念.关系就是定义两个或多个对象之间是如何关联的.它是由关系两端的多样性值识别的,比如,一对多意味着在关系的一端,只有一个实体,我们有时称为父母:在关

  • Entity Framework实体拆分多个表

    一.概念 实体拆分:一个实体拆分成多个表,如Product实体,可以拆分成Product和ProductWebInfo两个表,Product表用于存储商品的字符类信息,ProductWebInfo用于存储商品的图片信息,两张表通过SKU进行关联. 1.Product实体类结构: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Comp

  • Entity Framework使用DbModelBuilder API创建表结构

    DbContext类有一个OnModelCreating方法,它用于流利地配置领域类到数据库模式的映射.下面我们以fluent API的方式来定义映射.首先,先将Product类注释掉,重新编写该类,重新编写后的Product类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EFFluent

  • Entity Framework Core使用控制台程序生成数据库表

    目录 一.引言 1.添加实体类 2.添加Mircosoft.EntityFrameworkCore 二.生成数据库表 1.代码生成 2.程序包管理器控制台迁移 1.安装Microsoft.EntityFrameworkCore.Tools包 2.添加迁移 3.更新数据库 3.命令行迁移 一.引言 我们使用Code First的方式来生成数据库表,我们先讲解如何在控制台项目中生成数据库表. 在前面的文章中,我们是直接在控制台项目中安装的Mircosoft.EntityFrameworkCore,在

  • Entity Framework映射TPH、TPT、TPC与继承类

    目录 一.TPH 1.默认行为 2.Fluent API修改默认行为 二.TPT 1.默认行为 2.Fluent API修改默认行为 三.TPC 1.默认行为 2.Fluent API修改默认行为 四.实体拆分 五.表拆分 六.将类指定为复杂类型 1.指定方法: 2.配置复杂类型的属性 七.DataBase初始化 1.调用Database.SetInitializer方法: 2.通过配置文件更灵活的指定数据库初始化的方式: 3.自定义数据库初始化类 一.TPH Table Per Hierarc

  • Entity Framework使用LINQ操作实体

    一.什么是LINQ TO Entities LINQ,全称是Language-INtegrated Query(集成语言查询),是.NET语言中查询数据的一种技术.LINQ to Entities是一种机制,它促进了使用LINQ对概念模型的查询. 因为LINQ是声明式语言,它让我们聚焦于我们需要什么数据而不是应该如何检索数据.LINQ to Entities在实体数据模型之上提供了一个很好的抽象,所以我们可以使用LINQ来指定检索什么数据,然后LINQ to Entities provider会

  • Entity Framework使用Code First的实体继承模式

    目录 一.TPT继承模式 1.Person类 2.使用数据迁移创建数据库 3.填充数据 二.TPH模式 1.创建有继承关系的实体类 2.创建数据上下文 3.使用数据迁移创建数据库 4.不使用默认生成的区别多张表的类型 5.填充数据 6.查询数据 三.TPC模式 1.创建实体类 2.配置数据上下文 3.使用数据迁移生成数据库 4.填充数据 Entity Framework的Code First模式有三种实体继承模式 1.Table per Type (TPT)继承 2.Table per Clas

  • Entity Framework Core对Web项目生成数据库表

    一.引言 这篇文章中我们讲解如何在Web项目中使用EntityFrameworkCore,并生成数据库表,这里以ASP.NET Core WebApi为例讲解.还是采用分层的结构.创建后的项目整体结构如下图所示: 项目结构: EFCoreWeb.API:ASP.NET Core WebApi项目,用来提供Web功能,在项目中会引用EFCoreWeb.Data. EFCoreWeb.Data:类库项目,基于.NET Core的类库.存放的是与EFCore相关的操作. EFCoreWeb.Model

随机推荐