EF單實對應多表

 

1、單實體對應多表ide

  適用場景主表,擁有相同主鍵附屬表或擴展表。ui

  

 一、 建表詞句spa

  

CREATE TABLE [Chapter2].[Product](
    [SKU] [int] primary key ,
    [Description] [varchar](50) NOT NULL,
    [Price] [decimal](18, 2) NOT NULL)

CREATE TABLE [Chapter2].[ProductWebInfo](
    [SKU] [int] primary key ,
    [ImageURL] [varchar](1024) NULL
) 
View Code

 二、新建控制程序,添加EntityFramework 引用。3d

   三、建立Product實體,實體包含兩個表組合字段code

  

public class Product
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int SKU { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public string ImageURL { get; set; }
    }
View Code

 

   四、新建DbContext子類,注意鏈接字符串。     blog

public class ProductContext: DbContext
    {
        public DbSet<Product> Products { get; set; }

        public ProductContext() : base("EFRecipesEntities")
        {

        }


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

    注意代碼中,Map方法,由多個表組合成單個類配置。ip

    

 

      五、修改控制檯程序代碼ci

  

static void Main(string[] args)
        {
            using (var context = new ProductContext())
            {
                var product = new Product
                {
                    SKU = 147,
                    Description = "Expandable Hydration Pack",
                    Price = 19.97M,
                    ImageURL = "/pack147.jpg"
                };
                context.Products.Add(product);
                product = new Product
                {
                    SKU = 178,
                    Description = "Rugged Ranger Duffel Bag",
                    Price = 39.97M,
                    ImageURL = "/pack178.jpg"
                };
                context.Products.Add(product);
                product = new Product
                {
                    SKU = 186,
                    Description = "Range Field Pack",
                    Price = 98.97M,
                    ImageURL = "/noimage.jp"
                };
                context.Products.Add(product);
                product = new Product
                {
                    SKU = 202,
                    Description = "Small Deployment Back Pack",
                    Price = 29.97M,
                    ImageURL = "/pack202.jpg"
                };
                context.Products.Add(product);
                context.SaveChanges();
            }
            using (var context = new ProductContext())
            {
                foreach (var p in context.Products)
                {
                    Console.WriteLine("{0} {1} {2} {3}", p.SKU, p.Description,
                        p.Price.ToString("C"), p.ImageURL);
                }
            }

            Console.ReadKey();
        }
View Code

  運行使用SQLProfile查看查詢執行語句及結果。字符串

 

總結:單實體多表時,重寫DbContext字類,OnModelCreating方法,在方法中映射(註冊)實體屬性使用哪些表字段填充。get

相關文章
相關標籤/搜索