亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

一、概念

實(shí)體拆分:一個(gè)實(shí)體拆分成多個(gè)表,如Product實(shí)體,可以拆分成Product和ProductWebInfo兩個(gè)表,Product表用于存儲(chǔ)商品的字符類信息,ProductWebInfo用于存儲(chǔ)商品的圖片信息,兩張表通過SKU進(jìn)行關(guān)聯(lián)。

1、Product實(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 實(shí)體拆分.Model
{
    public class Product
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)] //設(shè)置主鍵需要自己填充
        public int SKU { get; set; }
        public string Description { get; set; }

        public decimal Price { get; set; }

        public string ImageURL { get; set; }
    }
}

 2、數(shù)據(jù)實(shí)體類結(jié)構(gòu):

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 實(shí)體拆分.Model;

namespace 實(shí)體拆分.DatabaseContext
{
    public class EFDbContext :DbContext
    {
        public EFDbContext()
            : base("name=Default")
        { }


        public DbSet<Product> Products { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>().Map(p =>
            {
                p.Properties(m => new { m.SKU, m.Price, m.Description });
                p.ToTable("Product");
            })
            .Map(p =>
            {
                p.Properties(m => new { m.SKU, m.ImageURL });
                p.ToTable("ProductWebInfo");
            });


            base.OnModelCreating(modelBuilder);
        }
    }
}

 3、使用數(shù)據(jù)遷移生成數(shù)據(jù)庫(kù),生成后的表結(jié)構(gòu)如下圖所示:

4、測(cè)試數(shù)據(jù):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 實(shí)體拆分.DatabaseContext;

namespace 實(shí)體拆分
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new EFDbContext())
            {
                context.Products.Add(new Model.Product() {
                    SKU=293,
                    Description="C#高級(jí)編程(第10版)",
                    Price=299 ,
                    ImageURL="http://image.baidu.com/1.jpg"
                });
                // 保存
                context.SaveChanges();
            }

            Console.WriteLine("創(chuàng)建成功");
            Console.ReadKey();
        }
    }
}

 5、運(yùn)行程序,查詢數(shù)據(jù)庫(kù)結(jié)果

總結(jié)

將實(shí)體拆分成多表的步驟:

1、在工程中創(chuàng)建一個(gè)新類繼承自DbContext類。
2、創(chuàng)建Product的POCO類。
3、在新創(chuàng)建的DbContext子類中添加屬性:DbSet<Product>。
4、重寫DbContext類的OnModelCreating()方法。

點(diǎn)此下載示例代碼

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

相關(guān)文章

最新評(píng)論