Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of splitting multiple tables by Entity Framework entity

Shulou Source: shulou.com Published: 2022-06-01 07:33:44 10月02日 Update

This article mainly shows you the "Entity Framework entity split multiple tables example analysis", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "Entity Framework entity split multiple tables example analysis" this article.

I. concept

Entity split: an entity is split into multiple tables, such as Product entities, which can be divided into Product and ProductWebInfo tables. Product table is used to store character information of goods, and ProductWebInfo is used to store image information of goods. The two tables are associated through SKU.

1. Product entity class structure: 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 entity split .Model {public class Product {[Key] [DatabaseGenerated (DatabaseGeneratedOption.None)] / / you need to fill in public int SKU {get; set;} public string Description {get; set to set the primary key. } public decimal Price {get; set;} public string ImageURL {get; set;}} 2, data entity class structure: using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;using entity split. Model Namespace entity split .DatabaseContext {public class EFDbContext: DbContext {public EFDbContext (): base ("name=Default") {} public DbSet Products {get; set;} protected override void OnModelCreating (DbModelBuilder modelBuilder) {modelBuilder.Entity () .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. Use data migration to generate a database, and the generated table structure is shown in the following figure:

4. Test data: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using entity split. DatabaseContext Namespace entity split {class Program {static void Main (string [] args) {using (var context = new EFDbContext ()) {context.Products.Add (new Model.Product () {SKU=293, Description= "C# Advanced programming (10th Edition)", Price=299 ImageURL= "http://image.baidu.com/1.jpg"}) / / Save context.SaveChanges ();} Console.WriteLine ("created successfully"); Console.ReadKey ();} 5. Run the program to query the database results.

The above is all the contents of the article "sample Analysis of Entity Framework entity splitting multiple tables". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Entities data multiple examples analysis content articles structures goods databases storage learning help generation advanced success two information pictures characters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Shulou Technology vpn Apple