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[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、查询数据

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值