1

how can i Add DbSet to my dbContext class, programmatically. [

        public class MyDBContext : DbContext 
        {
          
            public MyDBContext() : base("MyCon")
            {
               

                Database.SetInitializer<MyDBContext>(new CreateDatabaseIfNotExists<MyDBContext>());

            }
       
//Do this part programatically:
            public DbSet<Admin> Admins { get; set; }        
            public DbSet<MyXosh> MyProperty { get; set; }

        }

][1]

i want to add my model classes by ((C# Code-DOM)) and of course i did. but now i have problem with creating DbSet properties inside my Context class ...

4
  • You can't. The DbSet's are properties of the DbContext, and you can't expand existing objects at runtime. However, you can get the DbSet's of any mapped types using the context.Set<T>() property. Commented Oct 1, 2016 at 8:58
  • so how can i create table dynamically if i cant create dbset inside db context ?...have you any solution for this? Commented Oct 1, 2016 at 9:17
  • you can go without DbSet's entirely, override your OnModelCreating and map only those types you are actually going to use. Commented Oct 1, 2016 at 14:37
  • @DevilSuichiro Yes you are right ..I did it Commented Oct 2, 2016 at 5:48

2 Answers 2

2

yes i did!.. this: https://romiller.com/2012/03/26/dynamically-building-a-model-with-code-first/

And this: Create Table, Run Time using entity framework Code-First

are solution. no need to dispute with dbSets directly. it just works by do some thing like that:

  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);
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution but why didn't you just create also MyDBContext with codedom?
this solution is better, it doesn't need amount of code and generate a new dbcontext class for each model creating. @bubi
1

For those using EF Core that stubble here:

The code below is only for one table with the generic type. If you want more types you can always pass them through the constructor and run a cycle.

public class TableContextGeneric<T> : DbContext where T : class
{
    private readonly string _connectionString;

    //public virtual DbSet<T> table { get; set; }

    public TableContextGeneric(string connectionString)
    {
        _connectionString = connectionString;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var entityMethod = typeof(ModelBuilder).GetMethods().First(e => e.Name == "Entity");

        //the cycle will be run here
        entityMethod?.MakeGenericMethod(typeof(T))
            .Invoke(modelBuilder, new object[] { });

        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(_connectionString); // can be anyone 
    }
}

2 Comments

Could you provide an usage example please?
create a database based on certain types. Pratical example: Automatic invoice database creation based on the fields that you pass them. Using the code above, you just need to have this types and you can create a database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.