ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:79.87KB ,
资源ID:5363183      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5363183.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(EF Code First关系规则及配置.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

EF Code First关系规则及配置.docx

1、EF Code First关系规则及配置1、一对多关系关系表:Category 分类表Product 产品表分类与产品之间的一对多关系1、产品实体类不指定外键属性Domain中类定义:Category.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.ComponentModel.DataAnnotations; 7 8 namespace Northwind.Domain.Entities 9 10 public c

2、lass Category11 12 / 13 / 分类ID14 / 15 public int CategoryID get; set; 16 17 / 18 / 分类名称19 / 20 public string CategoryName get; set; 21 22 / 23 / 描述24 / 25 public string Description get; set; 26 27 / 28 / 图片29 / 30 public byte Picture get; set; 31 32 / 33 / 产品34 / 35 public virtual ICollection Produc

3、ts get; set; 36 37 Product.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Northwind.Domain.Entities 7 8 public class Product 9 10 / 11 / 产品ID12 / 13 public int ProductID get; set; 14 15 / 16 / 产品名称17 / 18 public string ProductName get;

4、set; 19 20 / 21 / 单价22 / 23 public decimal UnitPrice get; set; 24 25 / 26 / 库存27 / 28 public int UnitsInStock get; set; 29 30 / 31 / 是否售完32 / 33 public bool Discontinued get; set; 34 35 / 36 / 产品分类37 / 38 public virtual Category Category get; set; 39 40 CategoryMap.cs 1 using System; 2 using System.

5、Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity.ModelConfiguration; 7 using System.ComponentModel.DataAnnotations; 8 9 using Northwind.Domain.Entities;10 11 namespace Northwind.Domain.Mapping12 13 public class CategoryMap : EntityTypeConfiguration14 15 pub

6、lic CategoryMap()16 17 this.ToTable(dbo.Category);18 this.HasKey(t = t.CategoryID);19 20 this.Property(t = t.CategoryName).IsRequired().HasMaxLength(15);21 this.Property(t = t.Picture).HasColumnType(image);22 23 24 ProductMap.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq

7、; 4 using System.Text; 5 6 using System.Data.Entity.ModelConfiguration; 7 using System.ComponentModel.DataAnnotations; 8 9 using Northwind.Domain.Entities;10 11 namespace Northwind.Domain.Mapping12 13 public class ProductMap : EntityTypeConfiguration14 15 public ProductMap()16 17 this.ToTable(dbo.Pr

8、oduct);18 this.HasKey(t = t.ProductID);19 20 this.Property(t = t.ProductName).IsRequired().HasMaxLength(50);21 this.Property(t = t.UnitPrice).HasPrecision(18, 2);22 23 24 Data中类定义:NorthwindContext.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 us

9、ing System.Data.Entity; 7 8 using Northwind.Domain.Entities; 9 using Northwind.Domain.Mapping;10 11 namespace Northwind.Data12 13 public class NorthwindContext : DbContext14 15 public DbSet Categories get; set; 16 public DbSet Products get; set; 17 18 protected override void OnModelCreating(DbModelB

10、uilder modelBuilder)19 20 modelBuilder.Configurations.Add(new CategoryMap();21 modelBuilder.Configurations.Add(new ProductMap();22 23 24 App中类定义:Program.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using Northwind.Data; 7 using Northwind.Domain

11、.Entities; 8 9 using System.Data.Entity;10 11 namespace Northwind.App12 13 class Program14 15 static void Main(string args)16 17 / 数据模型改变,删除数据库重新创建。18 Database.SetInitializer(new DropCreateDatabaseIfModelChanges();19 20 Category c = new Category() CategoryName = 电子数码 ;21 22 Product p = new Product()

12、 ProductName = 笔记本电脑, UnitPrice = 4500.00m, Category = c, UnitsInStock = 100, Discontinued = false ;23 using (NorthwindContext db = new NorthwindContext()24 25 db.Categories.Add(c);26 db.Products.Add(p);27 28 db.SaveChanges();29 30 31 Console.WriteLine(Finish);32 Console.ReadKey();33 34 35 运行之后生成的数据

13、库结构2、产品实体类中指定外键属性修改Domain中Product.cs代码: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Northwind.Domain.Entities 7 8 public class Product 9 10 / 11 / 产品ID12 / 13 public int ProductID get; set; 14 15 / 16 / 产品名称17 / 18 public string Product

14、Name get; set; 19 20 / 21 / 单价22 / 23 public decimal UnitPrice get; set; 24 25 / 26 / 库存27 / 28 public int UnitsInStock get; set; 29 30 / 31 / 是否售完32 / 33 public bool Discontinued get; set; 34 35 / 36 / 分类ID37 / 38 public int CategoryID get; set; 39 40 / 41 / 产品分类42 / 43 public virtual Category Cate

15、gory get; set; 44 45 运行之后生成的数据库中表结构如下:默认的外键规则:Target Type Key Name, Target Type Name + Target Type Key Name, 或者 NavigationProperty Name + Target Type Key Name。3、使用Data Annotations指定外键属性 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Com

16、ponentModel.DataAnnotations; 7 8 namespace Northwind.Domain.Entities 9 10 public class Product11 12 / 13 / 产品ID14 / 15 public int ProductID get; set; 16 17 / 18 / 产品名称19 / 20 public string ProductName get; set; 21 22 / 23 / 单价24 / 25 public decimal UnitPrice get; set; 26 27 / 28 / 库存29 / 30 public i

17、nt UnitsInStock get; set; 31 32 / 33 / 是否售完34 / 35 public bool Discontinued get; set; 36 37 / 38 / 分类ID39 / 40 public int CategoryID get; set; 41 42 / 43 / 产品分类44 / 45 ForeignKey(CategoryID)46 public virtual Category Category get; set; 47 48 4、使用Fluent指定外键属性 1 using System; 2 using System.Collection

18、s.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity.ModelConfiguration; 7 using System.ComponentModel.DataAnnotations; 8 9 using Northwind.Domain.Entities;10 11 namespace Northwind.Domain.Mapping12 13 public class ProductMap : EntityTypeConfiguration14 15 public Product

19、Map()16 17 / Primary Key18 this.HasKey(t = t.ProductID);19 20 / Properties21 this.Property(t = t.ProductName).IsRequired().HasMaxLength(50);22 this.Property(t = t.UnitPrice).HasPrecision(18, 2);23 24 / Table & Column Mappings25 this.ToTable(dbo.Product);26 this.Property(t = t.ProductID).HasColumnNam

20、e(ProductID);27 this.Property(t = t.ProductName).HasColumnName(ProductName);28 this.Property(t = t.UnitPrice).HasColumnName(UnitPrice);29 this.Property(t = t.UnitsInStock).HasColumnName(UnitsInStock);30 this.Property(t = t.Discontinued).HasColumnName(Discontinued);31 this.Property(t = t.CategoryID).

21、HasColumnName(CategoryID);32 33 / Relationships34 this.HasRequired(t = t.Category)35 .WithMany(t = t.Products)36 .HasForeignKey(t = t.CategoryID)37 .WillCascadeOnDelete(false);38 39 40 5、示例代码附件以上示例代码附件,并补充Product与Category及Supplier的两个外键关联。Northwind-一对多外键.rar二、多对多关系表说明:用户表:User角色表:Role用户与角色多对多,一个用户可以属

22、于多个角色,一个角色可以有多个用户。Domain中User.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Northwind.Domain.Entities 7 8 public class User 9 10 / 11 / 用户ID12 / 13 public int UserID get; set; 14 15 / 16 / 用户名17 / 18 public string UserName get; set; 19

23、 20 / 21 / 密码22 / 23 public string Password get; set; 24 25 / 26 / 角色27 / 28 public ICollection Roles get; set; 29 30 Role.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Northwind.Domain.Entities 7 8 public class Role 9 10 / 11 / 角色ID12

24、 / 13 public int RoleID get; set; 14 15 / 16 / 角色名称17 / 18 public string RoleName get; set; 19 20 / 21 / 用户22 / 23 public virtual ICollection Users get; set; 24 25 UserMap.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity.Mo

25、delConfiguration; 7 using System.ComponentModel.DataAnnotations; 8 9 using Northwind.Domain.Entities;10 11 namespace Northwind.Domain.Mapping12 13 public class UserMap : EntityTypeConfiguration14 15 public UserMap()16 17 / Primary Key18 this.HasKey(t = t.UserID);19 20 / Properties21 this.Property(t = t.UserName).IsRequired().HasMaxLength(50);22 this.Property(t = t.Password).IsRequired().HasMaxLength

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1