Entity Framework表拆分为多个实体

 更新时间:2022年03月05日 09:44:04   作者:.NET开发菜鸟  
这篇文章介绍了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
{
    /// <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[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表拆分为多个实体的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • ADO.NET基础知识汇总

    ADO.NET基础知识汇总

    程序和数据库交互,要通过ADO.NET进行;通过ADO.NET就能在数据库中执行SQL了 。ADO.NET中提供了对不同数据库的统一操作接口(ODBC) 。另外还有一种操作数据库的接口是JDBC
    2015-11-11
  • 大早上更新了Visual Studio 2019  试用一下

    大早上更新了Visual Studio 2019 试用一下

    本文给大家分享一篇关于Visual Studio 2019 的相关知识,非常不错,具有一定的参考借鉴,需要的朋友可以参考下
    2019-04-04
  • c# 执行事务函数代码

    c# 执行事务函数代码

    c#下 执行多条sql语句,实现事务
    2009-05-05
  • .NET使用System.Timers.Timer类实现程序定时执行

    .NET使用System.Timers.Timer类实现程序定时执行

    这篇文章介绍了.NET使用System.Timers.Timer类实现程序定时执行的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • 配置Spring.Net框架开发环境

    配置Spring.Net框架开发环境

    这篇文章介绍了配置Spring.Net框架开发环境的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-03-03
  • asp.net利用cookie保存用户密码实现自动登录的方法

    asp.net利用cookie保存用户密码实现自动登录的方法

    这篇文章主要介绍了asp.net利用cookie保存用户密码实现自动登录的方法,实例分析了asp.net针对cookie的创建、提取与销毁操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • 如何在ASP.NET Core中使用ViewComponent

    如何在ASP.NET Core中使用ViewComponent

    这篇文章主要介绍了如何在ASP.NET Core中使用ViewComponent,帮助大家更好的理解和学习使用.net技术,感兴趣的朋友可以了解下
    2021-04-04
  • 利用Asp.Net Core的MiddleWare思想如何处理复杂业务流程详解

    利用Asp.Net Core的MiddleWare思想如何处理复杂业务流程详解

    这篇文章主要给大家介绍了关于利用Asp.Net Core的MiddleWare思想如何处理复杂业务流程的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧
    2018-08-08
  • .net中string无重复数字的实现方法

    .net中string无重复数字的实现方法

    今天做项目的时候,用js获得了勾选的checkbox放在了hiddenfile里,然而hiddenfile的值变成了类似:“1,1,1,3,3,2,4,5,5,5”,后台获取的时候,只保留不重复的数字,于是想了一想;直接上代码了。
    2013-04-04
  • ASP.NET MVC分页和排序功能实现

    ASP.NET MVC分页和排序功能实现

    这篇文章主要介绍了MVC学习系列之分页和排序功能实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07

最新评论