Entity Framework表拆分
一、概念
表拆分:一个表拆分成多个实体,例如Photograph表,可以拆分为Photograph和PhotographFullImage两张表。
Photograph实体结构:
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 CodeFirstTableSplit.Model
{
/// <summary>
/// 缩略图类
/// </summary>
public class Photograph
{
/// <summary>
/// 设置PhotoId是主键 自动增长
/// </summary>
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int PhotoId { get; set; } public string Title { get; set; } /// <summary>
/// 缩略图
/// </summary>
public byte[] ThumbnailBite { get; set; } /// <summary>
/// Photograph通过导航属性引用PhotographFullImage
/// </summary>
[ForeignKey("PhotoId")]
public virtual PhotographFullImage PhotographFullImage { get; set; }
}
}
2、PhotographFullImage实体结构:
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 CodeFirstTableSplit.Model
{
public class PhotographFullImage
{
[Key]
public int PhotoId { get; set; } /// <summary>
/// 高分辨率
/// </summary>
public byte[] HighResolutionBits { get; set; } /// <summary>
/// PhotographFullImage通过导航属性引用Photograph
/// </summary>
[ForeignKey("PhotoId")]
public virtual Photograph Photograph { get; set; }
}
}
3、创建数据上下文对象子类:
using CodeFirstTableSplit.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CodeFirstTableSplit.DatabaseContext
{
public class EFDbContext :DbContext
{
public EFDbContext()
: base("name=Default")
{ } public DbSet<Photograph> Photographs { get; set; } public DbSet<PhotographFullImage> PhotographFullImages { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// 设置主体
modelBuilder.Entity<Photograph>().HasRequired(p => p.PhotographFullImage).WithRequiredPrincipal(t => t.Photograph); // 生成同一张表:设置两个实体有相同的表名
modelBuilder.Entity<Photograph>().ToTable("Photograph");
modelBuilder.Entity<PhotographFullImage>().ToTable("Photograph");
base.OnModelCreating(modelBuilder);
} }
}
4、使用数据迁移生成数据库结构,查看生成后的结构:

5、写入数据
using CodeFirstTableSplit.DatabaseContext;
using CodeFirstTableSplit.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CodeFirstTableSplit
{
class Program
{
static void Main(string[] args)
{
using (var context = new EFDbContext())
{
// 写入数据
byte[] thumbBits = new byte[];
byte[] fullBits = new byte[];
var photo = new Photograph() { Title = "李四", ThumbnailBite = thumbBits };
var fullImage = new PhotographFullImage() { HighResolutionBits = fullBits }; photo.PhotographFullImage = fullImage;
context.Photographs.Add(photo);
// 保存
context.SaveChanges();
}
Console.WriteLine("创建成功");
Console.ReadKey();
}
}
}
6、查询数据

Entity Framework表拆分的更多相关文章
- Entity Framework实体拆分
一.概念 实体拆分:一个实体拆分成多个表,如Product实体,可以拆分成Product和ProductWebInfo两个表,Product表用于存储商品的字符类信息,ProductWebInfo用于 ...
- Entity Framework表名默认自动变为复数形式等常见问题解决方法
今天使用了一下手写EntityFramework,发现一些常见的问题,做个记录: 1.以前使用模板生成不太在意的问题,就是在定义实体类时,如果没映射注释,自动映射的表名会变成复数形式 如:表名==&g ...
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...
- AppBox升级进行时 - 关联表查询与更新(Entity Framework)
AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. 关联表的查询操作 使用 Include 方法,我们可以在一次数据库查询中将关联 ...
- Entity Framework – (复数)Plural and (单数)Singular 表名Table names
By default, the Entity Framework will assume that all of the names of your tables in your database a ...
- 让Entity Framework启动不再效验__MigrationHistory表
Entity Framework中DbContext首次加载OnModelCreating会检查__MigrationHistory表,作为使用Code Frist编程模式,而实际先有数据库时,这种检 ...
- Entity FrameWork对有外键关联的数据表的添加操作
前天做了一个MVC Entity FrameWork项目,遇到有外键关联的数据编辑问题.当你编辑的时候,按照正常的逻辑,把每个字段的数据都对号入座了,然后点击保存按钮,本以为会顺理成章的编辑数据,但是 ...
- Oracle + Entity Framework 更新没有设置主键的表
最近用Entity Framework 开发的时候,发现一个问题,在默认情况下,EF不能对一个没有主键的表进行更新.插入和删除的动作. 那么,应该怎么处理没有主键的表呢? 我们打开这个表的edmx文件 ...
- Entity Framework - Func引起的数据库全表查询
原文:http://www.cnblogs.com/dudu/archive/2012/04/01/enitity_framework_func.html 使用 Entity Framework 最要 ...
随机推荐
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- H5版如何在微信外(非微信浏览器)进行微信支付技术方案
官方是支持在非微信内置浏览器中调起微信支付的!H5支付是基于公众号基础开发的一种非微信内浏览器支付方式(需要单独申请支付权限),可以满足在微信外的手机H5页面进行微信支付的需求.同时,由于H5链接传播 ...
- 【js】appendChild
appendChild主要是用来追加节点插入到最后:循环的时候由于不停的搬家导致length在改变. 使用for循环 <!Doctype html> <html xmlns= ...
- C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序
C#中缓存的使用 缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可: <%@ Outp ...
- python matplotlib.pyplot画矩形图 以及plt.gca()
plt的Rectangle参数: 第一个参数是坐标(x,y),即矩形的画图的起点坐标,这个起点坐标不是一味地从左下角开始画,而是对应整个图中坐标原点,即(0,0). 第二个参数是矩形宽度 第三个坐标是 ...
- 转:Web优化 及常用工具包
Web优化: 减少http请求 避免404错误 在html页面header加入缓存标签 Gzip压缩网页 减少cookie体积 使用外部的js和css 消减js和css 压缩js 使用css spri ...
- Linux中断 - IRQ number和中断描述符
一.前言 本文主要围绕IRQ number和中断描述符(interrupt descriptor)这两个概念描述通用中断处理过程.第二章主要描述基本概念,包括什么是IRQ number,什么是中断描述 ...
- Linux时间子系统(十四) tick broadcast framework
一.前言 在内核中,有cpuidle framework可以控制cpu的节电:当没有进程调度到该cpu上执行的时候,swapper进程粉墨登场,将该cpu会被推入到idle状态.当然CPU的idle状 ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
- java中异常和集合
1. java中处理错误情况有两种,1 Error,2 Exception error是无法处理的,Exception是可以处理的情况. Exception中又有两种情况,RuntimeExcep ...