Reputation: 3743
I want to add tables to a SQLCe database at runtime, since the tablenames are not static and known at compile time. I try 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 entity type Person is not part of the model for the current context.
Is it possible to add a DbSet at runtime to the DbContext without having to define that DbSet(with its schema) in the database?
When defining the DbContext statically with Person, EF will create the entire database and the tables on the fly, which is great.
For example:
foreach (var item in collection)
{
string tableName = "PersonTable_"+item.Name;
//Add a table with the name tableName to DbContext
}
Is this somehow possible with EF or do I have to create these with some other technique?
Thanks, Juergen
Upvotes: 5
Views: 21379
Reputation: 21
now in EF8, you can do it like that:
public DbSet<Person> List { get; set; }
public string tablename = "list";
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>()
.ToTable(tablename)
.HasKey(s => s.NameId);
}
https://learn.microsoft.com/zh-cn/ef/core/modeling/entity-types?tabs=fluent-api
Upvotes: 2
Reputation: 1
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();
}
}
}
}
Upvotes: 0
Reputation: 1774
Entity Framework does not support dynamic creation of tables. So if you're using Code-First you have to define your DbSets in your DbContext in order to create corresponding tables.
http://entityframework.codeplex.com/discussions/448452
There are some workarounds out there like this question
Entity Framework (Code First) - Dynamically Building a Model
But EF is not the best ORM for that, try ServiceStack.OrmLite (it's a lightweight ORM).
Upvotes: 1
Reputation: 5815
you could use following, it would create tables or update them as necessary, not sure if this would work in production since it drops the db when model changes
Database.SetInitializer<DataContext>(
new DropCreateDatabaseIfModelChanges<DataContext>());
or if could create your own implementation of System.Data.Entity.IDatabaseInitializer interface
Upvotes: 2