C # EF 4.1 Dynamically creating a table in DbContext

I want to add tables to the SQLCe database at runtime because table names are not static and are known at compile time. I am trying to do this with Entity Framework 4.1 and DbContext as follows:

public class PersonContext : DbContext { public PersonContext() : base("UnicornsCEDatabase") { } } public class Person { public int NameId { get; set; } public string Name { get; set; } } public class Program { static void Main(string[] args) { using (var db = new PersonContext()) { db.Database.Delete(); //Try to create table DbSet per = db.Set<Person>(); var per1 = new Person { NameId = 1, Name = "James"}; per.Add(per1); int recordsAffected = db.SaveChanges(); Console.WriteLine( "Saved {0} entities to the database, press any key to exit.", recordsAffected); Console.ReadKey(); } } } 

When trying to run this, it throws this error: The object type of Person is not part of the model for the current context.

Is it possible to add a DbSet at runtime to a DbContext without to determine what the DbSet (with its schema) is in the database?

When defining DbContext statically with Person, EF will create the entire database and tables on the fly, which is great.

For instance:

 foreach (var item in collection) { string tableName = "PersonTable_"+item.Name; //Add a table with the name tableName to DbContext } 

How is this possible with EF or do I need to create them using some other technique?

Thanks, Jurgen

+4
source share
3 answers

you could use the following, he would create tables or update them as needed, not sure if this will work during the production process, since it resets db when the model changes.

  Database.SetInitializer<DataContext>( new DropCreateDatabaseIfModelChanges<DataContext>()); 

or if you could create your own implementation of the System.Data.Entity.IDatabaseInitializer interface

+1
source

Entity Framework does not support dynamic table creation. Therefore, if you use Code-First, you must define your DbSets in your DbContext in order to create the corresponding tables.

http://entityframework.codeplex.com/discussions/448452

There are some workarounds like this one

Entity structure (code first) - Dynamic model building

But EF is not the best ORM for this, try ServiceStack.OrmLite (this is a lightweight ORM).

+1
source
 namespace ConsoleApplication31 { public class PersonContext : DbContext { public PersonContext() : base("UnicornsCEDatabase") { } public DbSet<Person> Persons { get; set; } } public class Person { public int PersonId { get; set; } public int NameId { get; set; } public string Name { get; set; } } public class Program { static void Main(string[] args) { using (var db = new PersonContext()) { db.Database.Delete(); //Try to create table DbSet per = db.Set<Person>(); var per1 = new Person { NameId = 1, Name = "James" }; per.Add(per1); int recordsAffected = db.SaveChanges(); Console.WriteLine( "Saved {0} entities to the database, press any key to exit.", recordsAffected); Console.ReadKey(); } } } } 
0
source

Source: https://habr.com/ru/post/1381318/


All Articles