Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of splitting multiple tables by Entity Framework entity

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report