Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of splitting Entity Framework Table into multiple entities

Shulou Source: shulou.com Published: 2022-06-01 07:34:03 10月01日 Update

Editor to share with you the Entity Framework table is divided into multiple entities of the example analysis, I hope you will learn something after reading this article, let's discuss it together!

Concept

Table split: a table is split into multiple entities, such as the Photograph table, which can be split into Photograph and PhotographFullImage tables.

1. Photograph entity 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 CodeFirstTableSplit.Model {/ thumbnail class / public class Photograph {/ set PhotoId is the primary key auto-growth / [Key] [DatabaseGenerated (System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public int PhotoId {get; set;} public string Title {get; set } / thumbnail / public byte [] ThumbnailBite {get; set;} / reference PhotographFullImage / [ForeignKey ("PhotoId")] public virtual PhotographFullImage PhotographFullImage {get; set;}} 2 through navigation attributes, PhotographFullImage entity 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 CodeFirstTableSplit.Model {public class PhotographFullImage {[Key] public int PhotoId {get; set;} / High Resolution / public byte [] HighResolutionBits {get; set } / PhotographFullImage references Photograph / / [ForeignKey ("PhotoId")] public virtual Photograph Photograph {get; set;}} 3 through navigation attributes, and creates a data context object subclass: 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 Photographs {get; set;} public DbSet PhotographFullImages {get; set;} protected override void OnModelCreating (DbModelBuilder modelBuilder) {/ / set principal modelBuilder.Entity (). HasRequired (p = > p.PhotographFullImage) .WithRequiredPrincipal (t = > t.Photograph) / / generate the same table: set two entities to have the same table name modelBuilder.Entity (). ToTable ("Photograph"); modelBuilder.Entity (). ToTable ("Photograph"); base.OnModelCreating (modelBuilder);}} 4. Generate the database structure using data migration to view the generated structure:

5. Write data 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 ()) {/ / write data byte [] thumbBits = new byte [100] Byte [] fullBits = new byte [2000]; var photo = new Photograph () {Title = "Li Si", ThumbnailBite = thumbBits}; var fullImage = new PhotographFullImage () {HighResolutionBits = fullBits}; photo.PhotographFullImage = fullImage; context.Photographs.Add (photo); / / Save context.SaveChanges () } Console.WriteLine ("created successfully"); Console.ReadKey ();} 6. Query data

After reading this article, I believe you have a certain understanding of "sample Analysis of Entity Framework Table split into multiple entities". If you want to know more about it, welcome to follow the industry information channel. Thank you for your reading!

Tags: Entities data structures multiple generation examples analysis attributes articles navigation same success up and down context two body resolution subclasses done object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Technology NVidia Shulou Information vpn