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 最要 ...
随机推荐
- python学习笔记——multiprocessing 多进程组件-队列Queue
1 消息队列 1.1 基本语法 消息队列:multiprocessing.Queue,Queue是对进程安全的队列,可以使用Queue实现对进程之间的数据传输:还有一个重要作用是作为缓存使用. Que ...
- selenium2.0关于python的常用函数
转: 新建实例driver = webdriver.Chrome() 1.获取当前页面的Url函数 方法:current_url 实例: driver.current_url 2.获取元素坐标 方法: ...
- Android 中日期对话框的应用
import java.util.Calendar; import android.widget.DatePicker; import android.app.DatePickerDialog; // ...
- Unix环境高级编程(八)进程关系
本章看后给人似懂非懂的感觉,主要是不知道实际当中如何去使用.通过前面几章的学习,每个进程都有一个父进程,当子进程终止时,父进程得到通知并取得子进程的退出状态.先将本章基本的知识点总结如下,日后再看时候 ...
- Unix域套接字简介
在Linux系统中,有很多进程间通信方式,套接字(Socket)就是其中的一种.但传统的套接字的用法都是基于TCP/IP协议栈的,需要指定IP地址.如果不同主机上的两个进程进行通信,当然这样做没什么问 ...
- tp表单的提交与验证
一.控制器如下 引用use app\index\model\User; //注意模型类名不能和控制器类名相同 public function index(){ return $this->fet ...
- python中杂乱问题
1 字符串格式化中的格式指定 format_spec ::= [[fill]align][sign][#][][width][,][.precision][type] fill ::= <any ...
- 【JEECG_3.7.1】列表多表头的设计
先看下多表头的设计: 在这个多表头的表单当中,我们可以按照从上到下和从左往右的划分方式,将表头划分成三行十列,分别是: 列表标签 人员信息.部门信息.工资.入职状态.创建日期.操作 名称.年龄.性别. ...
- composer安装第三方的库packagist.org(laravel框架引入第三方库)
建立composer.json composer require phpoffice/phpexcel //安装一个excel扩展库 composer require gregwar/captcha ...
- PHP 如何获取二维数组中某个key的集合(高性能查找)
分享下PHP 获取二维数组中某个key的集合的方法. 具体是这样的,如下一个二维数组,是从库中读取出来的. 代码: $user = array( 0 => array( 'id' => 1 ...