2

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?

2
  • Charles Mager's answer is correct and the only way if you do not want to go to the underlying ObjectContext or another DbContext. Just wanted to mention you should always (when possible) wrap your DbContext's in using block, so they can get disposed of properly. Commented Oct 2, 2016 at 14:51
  • You certainly can use Charles' answer, but why not just add theDbSet<> properties to your MyDBContext class, at least for the entity types you intend to directly query? Commented Oct 2, 2016 at 21:13

1 Answer 1

2

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

Db.Set<Entity>().Add(entity); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.