溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Entity?Framework表拆分為多個(gè)實(shí)體的示例分析

發(fā)布時(shí)間:2022-03-05 11:42:40 來源:億速云 閱讀:140 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Entity Framework表拆分為多個(gè)實(shí)體的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

概念

表拆分:一個(gè)表拆分成多個(gè)實(shí)體,例如Photograph表,可以拆分為Photograph和PhotographFullImage兩張表。

1、Photograph實(shí)體結(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
{
    /// <summary>
    /// 縮略圖類
    /// </summary>
    public class Photograph
    {
        /// <summary>
        /// 設(shè)置PhotoId是主鍵 自動(dòng)增長
        /// </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通過導(dǎo)航屬性引用PhotographFullImage
        /// </summary>
        [ForeignKey("PhotoId")]
        public virtual PhotographFullImage PhotographFullImage { get; set; }
    }
}

 2、PhotographFullImage實(shí)體結(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; }

        /// <summary>
        /// 高分辨率
        /// </summary>
        public byte[] HighResolutionBits { get; set; }

        /// <summary>
        /// PhotographFullImage通過導(dǎo)航屬性引用Photograph
        /// </summary>
        [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<Photograph> Photographs { get; set; }

        public DbSet<PhotographFullImage> PhotographFullImages { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // 設(shè)置主體
            modelBuilder.Entity<Photograph>().HasRequired(p => p.PhotographFullImage).WithRequiredPrincipal(t => t.Photograph);

            // 生成同一張表:設(shè)置兩個(gè)實(shí)體有相同的表名
            modelBuilder.Entity<Photograph>().ToTable("Photograph");
            modelBuilder.Entity<PhotographFullImage>().ToTable("Photograph");
            base.OnModelCreating(modelBuilder);
        }


    }
}

 4、使用數(shù)據(jù)遷移生成數(shù)據(jù)庫結(jié)構(gòu),查看生成后的結(jié)構(gòu):

Entity?Framework表拆分為多個(gè)實(shí)體的示例分析

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ù)

Entity?Framework表拆分為多個(gè)實(shí)體的示例分析

看完了這篇文章,相信你對“Entity Framework表拆分為多個(gè)實(shí)體的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI