CodeFirst中DbContext能够动态添加DbSet么
1个回答
展开全部
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data.Entity;
6 using System.ComponentModel.DataAnnotations;
7 using System.Data.Entity.ModelConfiguration;
8 using System.Data.Entity.ModelConfiguration.Configuration;
9
10 namespace CodeFirstDemo
11 {
12 /// <summary>
13 /// 实体注册Helper接口
14 /// </summary>
15 public interface IEntityRegstHelper
16 {
17 void RegTo(ConfigurationRegistrar confRegistrar);
18 }
19
20 // 客户
21 public class Customer
22 {
23 public int CustomerID { get; set; }
24 public String CustomerName { get; set; }
25 public string Address { get; set; }
26 }
27 // 客户实体的注册Helper
28 public class CustomerRegstHelper : IEntityRegstHelper
29 {
30 public void RegTo(ConfigurationRegistrar confRegistrar)
31 {
32 confRegistrar.Add<Customer>(new EntityTypeConfiguration<Customer>());
33 }
34 }
35
36
37 public class MyDbContext : DbContext
38 {
39 List<IEntityRegstHelper> entityRegstHelperlist;
40 // 添加实体注册
41 public void AddEntityRegHelper(IEntityRegstHelper r)
42 {
43 if (entityRegstHelperlist == null)
44 entityRegstHelperlist = new List<IEntityRegstHelper>();
45 entityRegstHelperlist.Add(r);
46 }
47
48 //public DbSet<Customer> Customers { get; set; }
49 protected override void OnModelCreating(DbModelBuilder modelBuilder)
50 {
51 // 动态地加入实体
52 if (entityRegstHelperlist != null)
53 {
54 foreach (IEntityRegstHelper r in entityRegstHelperlist)
55 r.RegTo(modelBuilder.Configurations);
56 }
57 }
58 }
59
60 class Program
61 {
62 static void Main(string[] args)
63 {
64 using (var db = new MyDbContext())
65 {
66 db.AddEntityRegHelper(new CustomerRegstHelper());
67
68 var c = new Customer()
69 {
70 CustomerName = "长沙市大风通讯有限公司",
71 Address = "长沙市"
72 };
73 db.Set<Customer>().Add(c);
74 db.SaveChanges();
75
76 var query = from customer in db.Set<Customer>()
77 select customer;
78 foreach (var cq in query)
79 {
80 Console.WriteLine("客户: {0}, 地址:{1}", cq.CustomerName, cq.Address);
81 }
82
83 Console.WriteLine("按任意键退出");
84 Console.ReadKey();
85 }
86 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data.Entity;
6 using System.ComponentModel.DataAnnotations;
7 using System.Data.Entity.ModelConfiguration;
8 using System.Data.Entity.ModelConfiguration.Configuration;
9
10 namespace CodeFirstDemo
11 {
12 /// <summary>
13 /// 实体注册Helper接口
14 /// </summary>
15 public interface IEntityRegstHelper
16 {
17 void RegTo(ConfigurationRegistrar confRegistrar);
18 }
19
20 // 客户
21 public class Customer
22 {
23 public int CustomerID { get; set; }
24 public String CustomerName { get; set; }
25 public string Address { get; set; }
26 }
27 // 客户实体的注册Helper
28 public class CustomerRegstHelper : IEntityRegstHelper
29 {
30 public void RegTo(ConfigurationRegistrar confRegistrar)
31 {
32 confRegistrar.Add<Customer>(new EntityTypeConfiguration<Customer>());
33 }
34 }
35
36
37 public class MyDbContext : DbContext
38 {
39 List<IEntityRegstHelper> entityRegstHelperlist;
40 // 添加实体注册
41 public void AddEntityRegHelper(IEntityRegstHelper r)
42 {
43 if (entityRegstHelperlist == null)
44 entityRegstHelperlist = new List<IEntityRegstHelper>();
45 entityRegstHelperlist.Add(r);
46 }
47
48 //public DbSet<Customer> Customers { get; set; }
49 protected override void OnModelCreating(DbModelBuilder modelBuilder)
50 {
51 // 动态地加入实体
52 if (entityRegstHelperlist != null)
53 {
54 foreach (IEntityRegstHelper r in entityRegstHelperlist)
55 r.RegTo(modelBuilder.Configurations);
56 }
57 }
58 }
59
60 class Program
61 {
62 static void Main(string[] args)
63 {
64 using (var db = new MyDbContext())
65 {
66 db.AddEntityRegHelper(new CustomerRegstHelper());
67
68 var c = new Customer()
69 {
70 CustomerName = "长沙市大风通讯有限公司",
71 Address = "长沙市"
72 };
73 db.Set<Customer>().Add(c);
74 db.SaveChanges();
75
76 var query = from customer in db.Set<Customer>()
77 select customer;
78 foreach (var cq in query)
79 {
80 Console.WriteLine("客户: {0}, 地址:{1}", cq.CustomerName, cq.Address);
81 }
82
83 Console.WriteLine("按任意键退出");
84 Console.ReadKey();
85 }
86 }
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询