一、概念

表拆分:一个表拆分成多个实体,例如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表拆分的更多相关文章

  1. Entity Framework实体拆分

    一.概念 实体拆分:一个实体拆分成多个表,如Product实体,可以拆分成Product和ProductWebInfo两个表,Product表用于存储商品的字符类信息,ProductWebInfo用于 ...

  2. Entity Framework表名默认自动变为复数形式等常见问题解决方法

    今天使用了一下手写EntityFramework,发现一些常见的问题,做个记录: 1.以前使用模板生成不太在意的问题,就是在定义实体类时,如果没映射注释,自动映射的表名会变成复数形式 如:表名==&g ...

  3. 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表

    创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...

  4. AppBox升级进行时 - 关联表查询与更新(Entity Framework)

    AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. 关联表的查询操作 使用 Include 方法,我们可以在一次数据库查询中将关联 ...

  5. 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 ...

  6. 让Entity Framework启动不再效验__MigrationHistory表

    Entity Framework中DbContext首次加载OnModelCreating会检查__MigrationHistory表,作为使用Code Frist编程模式,而实际先有数据库时,这种检 ...

  7. Entity FrameWork对有外键关联的数据表的添加操作

    前天做了一个MVC Entity FrameWork项目,遇到有外键关联的数据编辑问题.当你编辑的时候,按照正常的逻辑,把每个字段的数据都对号入座了,然后点击保存按钮,本以为会顺理成章的编辑数据,但是 ...

  8. Oracle + Entity Framework 更新没有设置主键的表

    最近用Entity Framework 开发的时候,发现一个问题,在默认情况下,EF不能对一个没有主键的表进行更新.插入和删除的动作. 那么,应该怎么处理没有主键的表呢? 我们打开这个表的edmx文件 ...

  9. Entity Framework - Func引起的数据库全表查询

    原文:http://www.cnblogs.com/dudu/archive/2012/04/01/enitity_framework_func.html 使用 Entity Framework 最要 ...

随机推荐

  1. ZOJ 3203 Light Bulb (三分查找)

    Light Bulb Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...

  2. 使用 HTML5 History 新特性增强 Ajax 的体验(转)

    一. 场景再现 如大家熟知,Ajax 可以实现页面的无刷新操作,但会造成两个与普通页面操作(有刷新地改变页面)有着明显差别的问题—— URL 没有修改以及无法使用前进.后退按钮.例如常见的 Ajax ...

  3. 再看C#中的托付和事件

    在一口一个设计模式--观察者模式中.我们已经知道怎样应用观察者模式,但通过近期的深入学习,发现观察者模式存在下面缺陷: 1.抽象通知者依赖于抽象观察者: 2.每一个详细观察者被调用的方法必须一致. 比 ...

  4. 关于FSMC地址线的理解

    http://www.openedv.com/thread-33759-1-1.html (出处: OpenEdv-开源电子网)

  5. 腾讯 OCR 情况

    OCR技术之检测篇 https://cloud.tencent.com/developer/article/1101342 OCR技术之数据篇 https://cloud.tencent.com/de ...

  6. Composer的下载安装

    下载地址 https://getcomposer.org/download/ php必须开启php_openssl.dll  在php.ini 1.下载 composer.phar 2.然后配置 ph ...

  7. 有关UITableviewCell 重用内存 内部解析

    重用实现分析 查看UITableView头文件,会找到NSMutableArray*  visiableCells,和NSMutableDictnery* reusableTableCells两个结构 ...

  8. 如何在 Visual Studio 2013 中调试.NET Framework 4.5.1 代码

    版本需求如标题,在 工具->选项->调试->常规 中,更改以下设置: 禁用:启用“仅我的代码”.逐过程执行属性和运算符(仅限托管).要求源文件与原始版本完全匹配 启用:启用 .NET ...

  9. 【Android】11.4 Fragment及其生命周期

    分类:C#.Android.VS2015: 创建日期:2016-02-22 一.简介 Android从3.0开始引入了fragment的概念,主要是为了支持在大屏幕上实现更为动态和灵活的UI设计,比如 ...

  10. node js 调试方法

    1. node-debug tutorial 大家对nodejs调试应该都比较头疼,至少我这个不用IDE写js的人很头疼这个,其实node的生态圈非常好 有非常好的工具和非常潮的开发方式 这里总结了3 ...