问题
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