码迷,mamicode.com
首页 > 其他好文 > 详细

EntityFramework之一对多关系(三)

时间:2016-05-02 15:34:29      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

上篇介绍了一对一关系,下面介绍下一对多关系代码编写。

1、新建model实体,Product是产品类,Order是订单,一个产品对应多个订单

 1     public class Product
 2     {
 3         public int Id { get; set; }
 4         public string Name { get; set; }
 5         public string Description { get; set; }
 6 
 7         public virtual ICollection<Order> Orders { get; set; }
 8     }
 9 
10     public class Order
11     {
12         public int Id { get; set; }
13         public int ProductId { get; set; }
14         public DateTime Date { get; set; }
15         public string ShippingAddress { get; set; }
16 
17         public virtual Product Product { get; set; }
18     }

 2、创建一个EntityContext并继承自DbContext

 1     public class EntityContext : DbContext
 2     {
 3         public EntityContext()
 4             : base("name=DBConnectionString")
 5         {
 6 
 7         }
 8 
 9         public DbSet<Product> Product { get; set; }
10         public DbSet<Order> Order { get; set; }
11 
12         protected override void OnModelCreating(DbModelBuilder modelBuilder)
13         {
14             modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
15             base.OnModelCreating(modelBuilder);
16         }
17     }

  3、在web.config文件中添加数据库连接字符串

1   <connectionStrings>
2     <add name="DBConnectionString" connectionString="Data Source=.;Initial Catalog=OneToMany;Integrated Security=true" providerName="System.Data.SqlClient" />
3   </connectionStrings>

 4、接着创建实体映射

 1     public class ProductMap : EntityTypeConfiguration<Product>
 2     {
 3         public ProductMap()
 4         {
 5             ToTable("Product");
 6             HasKey(p => p.Id);
 7             //这个同OrderMap中注释语句效果一样,默认是级联
 8             HasMany(p => p.Orders).WithRequired(o => o.Product).HasForeignKey(o => o.ProductId).WillCascadeOnDelete(false);
 9         }
10     }
11 
12     public class OrderMap : EntityTypeConfiguration<Order>
13     {
14         public OrderMap()
15         {
16             ToTable("Order");
17             HasKey(o => o.Id);
18             //WillCascadeOnDelete:不使用级联删除
19             //HasRequired(o => o.Product).WithMany(p => p.Orders).HasForeignKey(o => o.ProductId).WillCascadeOnDelete(false);
20         }
21     }

  5、我们写一些操作,让Code First生成数据库并验证数据是否新增

 1     EntityContext db = new EntityContext();
 2     var product = new Product() { Description = "电器", Name = "电磁炉" };
 3     db.Set<Product>().Add(product);
 4     db.SaveChanges();
 5 
 6     var order = new Order() { Date = DateTime.Now, ProductId = product.Id, ShippingAddress = "江西南昌" };
 7     db.Set<Order>().Add(order);
 8     db.SaveChanges();
 9     
10     //表连接
11     var list = db.Set<Product>().Include("Orders").ToList();

 6、数据库关系图

技术分享

 

EntityFramework之一对多关系(三)

标签:

原文地址:http://www.cnblogs.com/zuqing/p/5452334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!