Entity Framework表拆分为多个实体的示例分析
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,小编给大家分享一下Entity Framework表拆分为多个实体的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!概念表拆分:一个表拆分成多个实体,例如Photograph表
千家信息网最后更新 2025年02月02日Entity Framework表拆分为多个实体的示例分析
小编给大家分享一下Entity Framework表拆分为多个实体的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
概念
表拆分:一个表拆分成多个实体,例如Photograph表,可以拆分为Photograph和PhotographFullImage两张表。
1、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{ ////// 缩略图类 /// public class Photograph { ////// 设置PhotoId是主键 自动增长 /// [Key] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public int PhotoId { get; set; } public string Title { get; set; } ////// 缩略图 /// public byte[] ThumbnailBite { get; set; } ////// Photograph通过导航属性引用PhotographFullImage /// [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; } ////// 高分辨率 /// public byte[] HighResolutionBits { get; set; } ////// PhotographFullImage通过导航属性引用Photograph /// [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 DbSetPhotographs { get; set; } public DbSet PhotographFullImages { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // 设置主体 modelBuilder.Entity ().HasRequired(p => p.PhotographFullImage).WithRequiredPrincipal(t => t.Photograph); // 生成同一张表:设置两个实体有相同的表名 modelBuilder.Entity ().ToTable("Photograph"); modelBuilder.Entity ().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[100]; byte[] fullBits = new byte[2000]; 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表拆分为多个实体的示例分析"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
实体
数据
结构
多个
生成
示例
分析
属性
篇文章
导航
相同
成功
上下
上下文
两个
主体
分辨率
子类
完了
对象
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
php数据库链接失败
网络安全评估第三版
计算机网络安全管理制度漏洞
金华网络技术有限公司招聘
郑州计算机网络技术学院
软件开发学习硬件
北京中宽宏运网络技术
如何创建中文版云数据库
数据库导入索引或主键不能包含
英特尔服务器存储市场再出绝招
可达鸭服务器和正式服有啥区别
软件开发及摊销如何做账
联想服务器保修期查询
国产数据库的发展有哪些
我的世界2b2t服务器怎么打
er图用在数据库设计的哪个步骤
马鞍山电商平台软件开发公司
魔兽3.35数据库
网络安全法健全用户信息制度
网络安全年报数据
潼南区企业网络技术服务定制价格
软件开发要求有哪些
迷你世界讨伐mc服务器
用本地服务器组建局域网
imc添加设备数据库错误
软件开发过程中常用的三层架构是
二级等保对网络安全设备的要求
实名认证网络安全吗
mysql数据库课题简述
c 常见软件开发面试题