Entity Framework表拆分为多个实体的示例分析

发布时间:2022-03-05 11:42:40 作者:小新
来源:亿速云 阅读:129

小编给大家分享一下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、使用数据迁移生成数据库结构,查看生成后的结构:

Entity Framework表拆分为多个实体的示例分析

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表拆分为多个实体的示例分析

看完了这篇文章,相信你对“Entity Framework表拆分为多个实体的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 实体框架(Entity Framework)简介
  2. Entity Framework 的事务 DbTransaction

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

entity framework

上一篇:Entity Framework如何管理一对二实体关系

下一篇:如何利用Python编写一个记忆翻牌游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》