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修改文件编码
http://topic.csdn.net/u/20080724/14/428de399-790d-442a-8340-3a5fb6dcfcee.html[修改文件编码,假设JS] 在Eclips ...
- Python学习笔记020——数据库中的数据类型
1 数值类型 数值类型分为有符号signed和无符号unsigned两种. 1.1 整型 int (1)bigint 极大整型(8个字节) 范围 :-2**64 ~ 2**64 - 1 -922337 ...
- DataGridView在Cell编辑状态响应回车键下的KeyPress/KeyDown/KeyUp事件
我们知道由于DataGridView的单元格DataGridCell处于编辑的时候,当你按Enter键,那么DataGridView是不会激发KewPress/KeyDown/KeyUp这些事件的,因 ...
- php 第三方DB库NOTORM
百度NOTORM找到该库的官网 :http://www.notorm.com/ 打开E:\AppServ\php7\php.ini 找到extension=php_pdo_mysql.dll 解开前面 ...
- ActiveMQ + NodeJS + Stomp 入门
NodeJS + stomp-client 入门 准备 下载ActiveMQ并安装 执行bin\win32\activemq.bat启动MQ服务 打开http://localhost:8161/adm ...
- xtrabackup迁移单独一张INNODB表
- SpringDaoSupport
@Component public class SuperDAO extends HibernateDaoSupport { @Resource(name="sessionFactory&q ...
- [k8s] kubelet单组件启动静态pod
kubelet单组件启动静态pod 无需k8s其他组件,单独下载kubelet的二进制,可以启动静态pod. 静态pod不受api管理,kubectl get po可以看到,但是kubectl del ...
- IOS5 ARC unsafe_unretained等说明
转自:http://blog.csdn.net/bsplover/article/details/7707964 iOS5中加入了新知识,就是ARC,其实我并不是很喜欢它,因为习惯了自己管理内存.但是 ...
- iOS之Sqlite3封装
一.代码下载 代码下载地址 二.实例效果展示 imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果图二.png&q ...