zoukankan      html  css  js  c++  java
  • EF多实体对应单表

    1、EF多实体对应单表

        适用场景:单数据库表,表数据有较长用字段,有不常用或者大数据字段。

    2、建表语句  

    CREATE TABLE [Chapter2].[Photograph](
        [PhotoId] [int] IDENTITY(1,1) primary key NOT NULL,
        [Title] [varchar](50) NOT NULL,
        [ThumbnailBits] [image] NOT NULL,
        [HighResolutionBits] [image] NOT NULL
        )
    View Code

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

     4、创建两个实体实体,实体由同一个表不同字段组成。

    public class PictureContext : DbContext
        {
            public DbSet<Photograph> Photographs { get; set; }
            public DbSet<PhotographFullImage> PhotographFullImage { get; set; }
    
            public PictureContext() : base("EFRecipesEntities")
            {
    
            }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
    
                modelBuilder.Entity<Photograph>()
                    .HasRequired(p => p.PhotographFullImage)
                    .WithRequiredPrincipal(p => p.Photograph);
    
                modelBuilder.Entity<Photograph>().ToTable("Photograph", "Chapter2");
                modelBuilder.Entity<PhotographFullImage>().ToTable("Photograph", "Chapter2");
            }
        }
    
        public class Photograph
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int PhotoId { get; set; }
            public string Title { get; set; }
            public byte[] ThumbnailBits { get; set; }
            [ForeignKey("PhotoId")]
            public virtual PhotographFullImage PhotographFullImage { get; set; }
        }
    
        public class PhotographFullImage
        {
            [Key]
            public int PhotoId { get; set; }
            public byte[] HighResolutionBits { get; set; }
            [ForeignKey("PhotoId")]
            public virtual Photograph Photograph { get; set; }
        }
    View Code

     注意图中指定Photograph实体需要(HasRequired) PhotographFullImage实体,并通过WithRequiredPrincipal,指定主键由Photograph负责。

    5、修改Main程序如下:

      

     static void Main(string[] args)
            {
                byte[] thumbBits = new byte[100];
                byte[] fullBits = new byte[2000];
                using (var context = new PictureContext())
                {
                    var photo = new Photograph
                    {
                        Title = "My Dog",
                        ThumbnailBits = thumbBits
                    };
                    var fullImage = new PhotographFullImage { HighResolutionBits = fullBits };
                    photo.PhotographFullImage = fullImage;
                    context.Photographs.Add(photo);
                    context.SaveChanges();
                }
                using (var context = new PictureContext())
                {
                    foreach (var photo in context.Photographs)
                    {
                        Console.WriteLine("Photo: {0}, ThumbnailSize {1} bytes",
                            photo.Title, photo.ThumbnailBits.Length);
                        // explicitly load the "expensive" entity,
                        context.Entry(photo).Reference(p => p.PhotographFullImage).Load();
                        Console.WriteLine("Full Image Size: {0} bytes",
                            photo.PhotographFullImage.HighResolutionBits.Length);
                    }
                }
    
                Console.ReadKey();
            }
    View Code
  • 相关阅读:
    解决本地浏览器的跨域问题
    页面嵌入iframe关于父子传参调用
    仿微信、qq聊天,@好友功能
    创建新react项目 运行npm start 报错踩过的坑
    前端向后端获取数据的三种方法:ajax、axios、fetch
    观察者模式代码详解
    代理模式实现图片预加载、懒加载
    网站登录注册-Session 和token的总结
    浅谈MVC、MVVM的区别
    vue中methods、computed、watch区别
  • 原文地址:https://www.cnblogs.com/bro-ma/p/10703890.html
Copyright © 2011-2022 走看看