码迷,mamicode.com
首页 > 数据库 > 详细

CodeFirst中DbContext动态添加DbSet

时间:2016-12-29 20:01:56      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:one   .config   for   ons   pac   cti   set   line   hang   

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Data.Entity;
 using System.ComponentModel.DataAnnotations;
 using System.Data.Entity.ModelConfiguration;
 using System.Data.Entity.ModelConfiguration.Configuration;
 
 namespace CodeFirstDemo
 {
     /// <summary>
     /// 实体注册Helper接口
     /// </summary>
     public interface IEntityRegstHelper
     {
         void RegTo(ConfigurationRegistrar confRegistrar);
     }
 
     // 客户
     public class Customer
     {
         public int CustomerID { get; set; }
         public String CustomerName { get; set; }
         public string Address { get; set; }
     }
     // 客户实体的注册Helper
     public class CustomerRegstHelper : IEntityRegstHelper
     {
         public void RegTo(ConfigurationRegistrar confRegistrar)
         {
             confRegistrar.Add<Customer>(new EntityTypeConfiguration<Customer>());
         }
     }
     
     
     public class MyDbContext : DbContext
     {
         List<IEntityRegstHelper> entityRegstHelperlist;
         // 添加实体注册
         public void AddEntityRegHelper(IEntityRegstHelper r)
         {
             if (entityRegstHelperlist == null)
                 entityRegstHelperlist = new List<IEntityRegstHelper>();
             entityRegstHelperlist.Add(r);
         }
     
         //public DbSet<Customer> Customers { get; set; }
         protected override void OnModelCreating(DbModelBuilder modelBuilder)
         {
             // 动态地加入实体
             if (entityRegstHelperlist != null)
             {
                 foreach (IEntityRegstHelper r in entityRegstHelperlist)
                     r.RegTo(modelBuilder.Configurations);
             }
         }
     }
     
     class Program
     {
         static void Main(string[] args)
         {
             using (var db = new MyDbContext())
             {
                 db.AddEntityRegHelper(new CustomerRegstHelper());
                 
                 var c = new Customer()
                 {
                     CustomerName = "长沙市大风通讯有限公司",
                     Address = "长沙市"
                 };
                 db.Set<Customer>().Add(c);
                 db.SaveChanges();
 
                 var query = from customer in db.Set<Customer>()
                             select customer;
                 foreach (var cq in query)
                 {
                     Console.WriteLine("客户: {0}, 地址:{1}", cq.CustomerName, cq.Address);
                 }
                 
                 Console.WriteLine("按任意键退出");
                 Console.ReadKey();
             }
         }
     }
 }

 

CodeFirst中DbContext动态添加DbSet

标签:one   .config   for   ons   pac   cti   set   line   hang   

原文地址:http://www.cnblogs.com/answercard/p/6234562.html

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