这篇文章主要为大家展示了“Entity Framework实体拆分多个表的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Entity Framework实体拆分多个表
这篇文章主要为大家展示了“Entity Framework实体拆分多个表的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Entity Framework实体拆分多个表的示例分析”这篇文章吧。
实体拆分:一个实体拆分成多个表,如Product实体,可以拆分成Product和ProductWEBInfo两个表,Product表用于存储商品的字符类信息,ProductWebInfo用于存储商品的图片信息,两张表通过SKU进行关联。
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 实体拆分.Model{ public class Product { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] //设置主键需要自己填充 public int SKU { get; set; } public string Description { get; set; } public decimal Price { get; set; } public string ImageURL { get; set; } }}
using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;using 实体拆分.Model;namespace 实体拆分.DatabaseContext{ public class EFDbContext :DbContext { public EFDbContext() : base("name=Default") { } public DbSet<Product> Products { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Product>().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); } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using 实体拆分.DatabaseContext;namespace 实体拆分{ class Program { static void Main(string[] args) { using (var context = new EFDbContext()) { context.Products.Add(new Model.Product() { SKU=293, Description="C#高级编程(第10版)", Price=299 , ImageURL="Http://image.baidu.com/1.jpg" }); // 保存 context.SaveChanges(); } Console.WriteLine("创建成功"); Console.ReadKey(); } }}
以上是“Entity Framework实体拆分多个表的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!
--结束END--
本文标题: Entity Framework实体拆分多个表的示例分析
本文链接: https://www.lsjlt.com/news/324030.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0