Iman Salehi
Iman Salehi

Reputation: 956

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

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?

Upvotes: 2

Views: 2666

Answers (1)

Charles Mager
Charles Mager

Reputation: 26213

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

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

Upvotes: 2

Related Questions