最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Entity Framework表拆分為多個(gè)實(shí)體

 更新時(shí)間:2022年03月05日 09:44:04   作者:.NET開發(fā)菜鳥  
這篇文章介紹了Entity Framework表拆分為多個(gè)實(shí)體的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

概念

表拆分:一個(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)增長(zhǎ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ù)上下文對(duì)象子類:

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ù)庫(kù)結(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ù)

到此這篇關(guān)于Entity Framework表拆分為多個(gè)實(shí)體的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ADO.NET基礎(chǔ)知識(shí)匯總

    ADO.NET基礎(chǔ)知識(shí)匯總

    程序和數(shù)據(jù)庫(kù)交互,要通過ADO.NET進(jìn)行;通過ADO.NET就能在數(shù)據(jù)庫(kù)中執(zhí)行SQL了 。ADO.NET中提供了對(duì)不同數(shù)據(jù)庫(kù)的統(tǒng)一操作接口(ODBC) 。另外還有一種操作數(shù)據(jù)庫(kù)的接口是JDBC
    2015-11-11
  • 大早上更新了Visual Studio 2019  試用一下

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

    本文給大家分享一篇關(guān)于Visual Studio 2019 的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒,需要的朋友可以參考下
    2019-04-04
  • c# 執(zhí)行事務(wù)函數(shù)代碼

    c# 執(zhí)行事務(wù)函數(shù)代碼

    c#下 執(zhí)行多條sql語(yǔ)句,實(shí)現(xiàn)事務(wù)
    2009-05-05
  • .NET使用System.Timers.Timer類實(shí)現(xiàn)程序定時(shí)執(zhí)行

    .NET使用System.Timers.Timer類實(shí)現(xiàn)程序定時(shí)執(zhí)行

    這篇文章介紹了.NET使用System.Timers.Timer類實(shí)現(xiàn)程序定時(shí)執(zhí)行的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • 配置Spring.Net框架開發(fā)環(huán)境

    配置Spring.Net框架開發(fā)環(huán)境

    這篇文章介紹了配置Spring.Net框架開發(fā)環(huán)境的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • asp.net利用cookie保存用戶密碼實(shí)現(xiàn)自動(dòng)登錄的方法

    asp.net利用cookie保存用戶密碼實(shí)現(xiàn)自動(dòng)登錄的方法

    這篇文章主要介紹了asp.net利用cookie保存用戶密碼實(shí)現(xiàn)自動(dòng)登錄的方法,實(shí)例分析了asp.net針對(duì)cookie的創(chuàng)建、提取與銷毀操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • 如何在ASP.NET Core中使用ViewComponent

    如何在ASP.NET Core中使用ViewComponent

    這篇文章主要介紹了如何在ASP.NET Core中使用ViewComponent,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下
    2021-04-04
  • 利用Asp.Net Core的MiddleWare思想如何處理復(fù)雜業(yè)務(wù)流程詳解

    利用Asp.Net Core的MiddleWare思想如何處理復(fù)雜業(yè)務(wù)流程詳解

    這篇文章主要給大家介紹了關(guān)于利用Asp.Net Core的MiddleWare思想如何處理復(fù)雜業(yè)務(wù)流程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起看看吧
    2018-08-08
  • .net中string無(wú)重復(fù)數(shù)字的實(shí)現(xiàn)方法

    .net中string無(wú)重復(fù)數(shù)字的實(shí)現(xiàn)方法

    今天做項(xiàng)目的時(shí)候,用js獲得了勾選的checkbox放在了hiddenfile里,然而hiddenfile的值變成了類似:“1,1,1,3,3,2,4,5,5,5”,后臺(tái)獲取的時(shí)候,只保留不重復(fù)的數(shù)字,于是想了一想;直接上代碼了。
    2013-04-04
  • ASP.NET MVC分頁(yè)和排序功能實(shí)現(xiàn)

    ASP.NET MVC分頁(yè)和排序功能實(shí)現(xiàn)

    這篇文章主要介紹了MVC學(xué)習(xí)系列之分頁(yè)和排序功能實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07

最新評(píng)論

昌乐县| 梁山县| 镇原县| 榆树市| 广平县| 旬阳县| 公安县| 乐都县| 嘉义市| 凉城县| 兴海县| 广西| 涞源县| 浙江省| 太原市| 通州市| 麻栗坡县| 沂水县| 乌恰县| 六枝特区| 廉江市| 姜堰市| 泸溪县| 东丽区| 舟山市| 高陵县| 神池县| 定远县| 黎城县| 洪洞县| 兴宁市| 巍山| 荥经县| 宜宾县| 铜山县| 疏勒县| 临沂市| 康平县| 柏乡县| 奇台县| 清镇市|