参考
Creating DbSet Properties Dynamically

C# code?
1
DbSet<MyEntity> set = context.Set<MyEntity>();

C# code?
1
DbSet set = context.Set( typeof( MyEntity ) );

或者利用反射,通过实现DbContext的OnModelCreating方法,参考
Dynamically Adding DbSet Properties in DbContext for Entity Framework Code First

引用

However, for a project with even a fair bit of domain complexity, defining DbSet properties for all entities would not be possible and we need a way to dynamically read our Entity Types and add them dynamically in the DbContext. This can be achieved by implementing OnModelCreating method of the DbContext. The Entity Types can be read from either the executing assembly Assembly.GetExecutingAssembly() or by loading more than one assemblies which can be placed in a configuration file (as shown below in the example). For details about setting up the custom configuration section with your assemblies see my last post

The following code will add all Class Types to the DbContext for all the assemblies added in the configuration file, notice we are actually calling modelBuilder.Entity<T>() method through reflection.

C# code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class MyAppContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            CustomAssemblySection configSection = (CustomAssemblySection)System.Configuration.ConfigurationManager.GetSection("CustomAssemblySection");
  
 
            foreach (CustomAssembly customAssembly in configSection.Assemblies)
            {
                Assembly assembly = Assembly.Load(customAssembly.Name);
                foreach (Type type in assembly.ExportedTypes)
                {
                    if (type.IsClass)
                    {
                        MethodInfo method = modelBuilder.GetType().GetMethod("Entity");
                        method = method.MakeGenericMethod(new Type[] { type });
                        method.Invoke(modelBuilder, null);
                    }
                }
            }
            base.OnModelCreating(modelBuilder);
        }
    }

DbContext运行时动态附加上一个dbset的更多相关文章

  1. 使用javassist运行时动态重新加载java类及其他替换选择

    在不少的情况下,我们需要对生产中的系统进行问题排查,但是又不能重启应用,java应用不同于数据库的存储过程,至少到目前为止,还不能原生的支持随时进行编译替换,从这种角度来说,数据库比java的动态性要 ...

  2. [转] Java运行时动态生成class的方法

    [From] http://www.liaoxuefeng.com/article/0014617596492474eea2227bf04477e83e6d094683e0536000 廖雪峰 / 编 ...

  3. SpringBoot运行时动态添加数据源

    此方案适用于解决springboot项目运行时动态添加数据源,非静态切换多数据源!!! 一.多数据源应用场景: 1.配置文件配置多数据源,如默认数据源:master,数据源1:salve1...,运行 ...

  4. 解决 Retrofit 多 BaseUrl 及运行时动态改变 BaseUrl ?

    原文地址: juejin.im/post/597856- 解决Retrofit多BaseUrl及运行时动态改变BaseUrl(一) 解决Retrofit多BaseUrl及运行时动态改变BaseUrl( ...

  5. Java 运行时动态生成class

    转载 http://www.liaoxuefeng.com/article/0014617596492474eea2227bf04477e83e6d094683e0536000 Java是一门静态语言 ...

  6. .NET6运行时动态更新限流阈值

    昨天博客园撑不住流量又崩溃了,很巧正在编写这篇文章,于是产生一个假想:如果博客园用上我这个限流组件会怎么样呢? 用户会收到几个429错误,并且多刷新几次就看到了内容,不会出现完全不可用. 还可以降低查 ...

  7. LINQ to SQL 运行时动态构建查询条件

    在进行数据查询时,经常碰到需要动态构建查询条件.使用LINQ实现这个需求可能会比以前拼接SQL语句更麻烦一些.本文介绍了3种运行时动态构建查询条件的方法.本文中的例子最终实现的都是同一个功能,从Nor ...

  8. 运行时动态库:not found 及介绍-linux的-Wl,-rpath命令

    ---此文章同步自我的CSDN博客--- 一.运行时动态库:not found   今天在使用linux编写c/c++程序时,需要用到第三方的动态库文件.刚开始编译完后,运行提示找不到动态库文件.我就 ...

  9. C++高效安全的运行时动态类型转换

    关键字:static_cast,dynamic_cast,fast_dynamic_cast,VS 2015. OS:Window 10. C++类之间类型转换有:static_cast.dynami ...

随机推荐

  1. python学习[二]

    继续学习 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html 词典的每个元素是键值对.元素没有顺序.dic = {'tom':11 ...

  2. nginx/apache/php隐藏http头部版本信息的实现方法

    有时候我们需要隐藏我们的服务器版本信息,防止有心人士的研究,更安全,这里介绍下在nginx/apache/php中如何隐藏http头部版本信息的方法. nginx隐藏头部版本信息方法 编辑nginx. ...

  3. 【mysql的编程专题③】内置函数

    数学函数 常用 abs(x) 返回x的绝对值 floor(x) 返回小于x的最大整数值 mod(x,y) 返回x/y的模(余数) rand() 返回0到1内的随机值,可以通过提供一个参数(种子)使ra ...

  4. posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序

    posix 线程(一):线程模型.pthread 系列函数 和 简单多线程服务器端程序 一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属 ...

  5. java nio知识点总结

    1.NIO是Java 4里面提供的新的API,目的是用来解决传统IO的问题.是用来解决传统io的问题的. 用来解决传统io的问题的.用来解决传统io的问题.阻塞的. 2.传统IO中,Stream是单向 ...

  6. servlet会话技术:Cookie

    什么是会话会话可以简单理解为:用户开一个浏览器访问某个网站,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话.会话过程中需要解决的一些问题每个用户在使用浏览器与服务器进 ...

  7. java.util.regex.PatternSyntaxException: Unclosed character class near index解决办法

    使用str.split("[")时,出现java.util.regex.PatternSyntaxException: Unclosed character class near  ...

  8. java抓取动态生成的网页

    最近在做项目的时候有一个需求:从网页面抓取数据,要求是首先抓取整个网页的html源码(后期更新要使用到).刚开始一看这个简单,然后就稀里哗啦的敲起了代码(在这之前使用过Hadoop平台的分布式爬虫框架 ...

  9. 【c++】中文设置

    #include <iostream> #include <string> #include<locale> using namespace std; ; int ...

  10. FileObverse文件观察者的Debug报告

    FileObverse文件观察者的Debug报告 2014年9月18日 9:03