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 最要 ...
随机推荐
- 配置Eclipse 3.3 + tomcat 6.0 + lomboz 3.3进行Web开发
http://www.cnblogs.com/xtsong/articles/1203155.html我以前编程用的都是Eclipse 3.2,这次跑到www.eclipse.org上去溜达了一番,发 ...
- Linux命令-实时监测命令:watch
watch 是一个非常实用的命令,基本所有的 Linux 发行版都带有这个小工具,如同名字一样,watch 可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行..在Linux下,watch是周期性 ...
- Unix环境高级编程(六)进程控制
本章介绍Unix的进程控制,包括进程创建,执行程序和进程终止,进程的属性,exec函数系列,system函数,进程会计机制. 1.进程标识符 每一个进程都有一个非负整数标识的唯一进程ID.ID为0表示 ...
- Python center() 方法
描述 center() 方法返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格. 语法 center() 方法语法: S.center(width[,fillch ...
- UHF RFID编码之TPP编码
GB/T 39768通信交互模型 读写器使用TPP对基带数据进行编码,使用DSB-ASK或者SSB-ASK方式调制射频载波,向一个或者多个标签发送命令.命令发送后,读写器继续发送未经调制的射频载波,并 ...
- Spring 中属性配置
1 注册自定义属性编辑器,方法一.使用BeanFactory, 则用户需要手动调用 registerCustomEditor(Class requiredType, PropertyEditor pr ...
- ubuntu中pip安装redis-py及pip的使用
安装redis-py的前提是已经将redis成功安装,redis安装过程请看博文 ubuntu14安装redis 1.安装pip sudo apt-get install python-pip 2.使 ...
- notepad++与ISE/Vivado关联
转自:http://www.cnblogs.com/ninghechuan/p/6172237.html 1.notepad++与vivado关联 打开vivado软件,选择菜单栏“Tools——&g ...
- Qt Quick + OpenGL + Bullet初次測试
Qt Quick + OpenGL + Bullet初次測试 眼下Qt的Quick模块已经表现得很出色,并且可以预留接口来渲染OpenGL场景.一般来说,已经可以满足大部分编程须要了.这次呢.尝试使用 ...
- CentOS 7 设置中文环境
在vultr上的虚拟机虽然安装了中文支持,但是默认显示英语. 只要修改 /etc/locale.conf 即可. LANG="zh_CN.UTF-8" LANGUAGE=" ...