日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Entity?Framework表拆分為多個實體_實用技巧

作者:.NET開發(fā)菜鳥 ? 更新時間: 2022-05-05 編程語言

概念

表拆分:一個表拆分成多個實體,例如Photograph表,可以拆分為Photograph和PhotographFullImage兩張表。

1、Photograph實體結(jié)構(gòu):

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實體結(jié)構(gòu):

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、創(chuàng)建數(shù)據(jù)上下文對象子類:

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 Photographs { 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、使用數(shù)據(jù)遷移生成數(shù)據(jù)庫結(jié)構(gòu),查看生成后的結(jié)構(gòu):

5、寫入數(shù)據(jù)

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())
            {
                // 寫入數(shù)據(jù)
                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("創(chuàng)建成功");
            Console.ReadKey();
        }
    }
}

?6、查詢數(shù)據(jù)

原文鏈接:https://www.cnblogs.com/dotnet261010/p/8007628.html

欄目分類
最近更新