In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
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: 229
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.