How to use DbContext when there is no DbSet<> inside that, for query?

天涯浪子 提交于 2019-12-10 11:23:58

问题


I'm using Entity Framework Code-First and this is my DbContext. As you see there is nothing about DbSet<> properties and all of that will provide from my model classes which are providing by C# CodeDOM. I'm creating my Tables dynamically by using Code-First.

    public class MyDBContext : DbContext
{

    public MyDBContext() : base("MyCon")
    {
        Database.SetInitializer<MyDBContext>(new CreateDatabaseIfNotExists<MyDBContext>());
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");
        var theList = Assembly.GetExecutingAssembly().GetTypes()
                  .Where(t => t.Namespace == "FullDynamicWepApp.Data.Domins")
                  .ToList();
        foreach (var item in theList)
        {
            entityMethod.MakeGenericMethod(item)
                           .Invoke(modelBuilder, new object[] { });
        }
        base.OnModelCreating(modelBuilder);
    }

}

but now I don't know how can I write my query Linq without any DbSet<> inside my DbContext? make instance of my DbContext and use which property?

 MyDBContext Db = new MyDBContext();
        Db.What???????

How can I write my CRUD operation s in these circumstances?


回答1:


The DbContext.Set<TEntity> method will return a DbSet<TEntity> for the given type, e.g:

Db.Set<Entity>().Add(entity);


来源:https://stackoverflow.com/questions/39817703/how-to-use-dbcontext-when-there-is-no-dbset-inside-that-for-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!