0

I already have a database with tables outside EF scope. But I want that the tables which will be used by EF to be created automatically.

public class SessionInfo
{
    public Guid Id {get;set;}
    public string Name { get; set; }
    public DateTime StartsOn { get; set; }
    public DateTime EndsOn { get; set; }
    public string Notes { get; set; }
}

public class StudentsDbContext:DbContext
{
    public StudentsDbContext():base("name=memory")

    {
        Database.Log = s => this.LogDebug(s);         

    }

    public DbSet<SessionInfo> Sessions { get; set; }
}

This code just throws an exception because the table SessionInfoes doesn't exist.

 using (var db = new StudentsDbContext())
 {
       db.Sessions.Add(new SessionInfo() {Id = Guid.NewGuid(), Name = "bla"});           
       var st = db.Sessions.FirstOrDefault();
 }

What do I need to do so that EF will create the "SessionInfoes" (whatever name, it's not important) table by itself? I was under the impression that Ef will create the tables when the context is first used for a change or a query.

Update

After some digging, it seems that EF and Sqlite don't play very nice together i.e at most you can use EF to do queries but that's it. No table creation, no adding entities.

MikeSW
  • 16,140
  • 3
  • 39
  • 53
  • You can generate an EDMX, see my answer to a similar question: http://stackoverflow.com/questions/25089346/database-first-create-entity-framework-6-1-1-model-using-system-data-sqlite-1-0 – TomL Aug 11 '14 at 14:25

1 Answers1

1

EF needs additional information in order to do this. You'll have to specify an IDatabaseInitializer first. Take a look at this list and find one that is appropriate for your needs (for example: MigrateDatabaseToLatestVersion, DropCreateDatabaseAlways, DropCreateDatabaseIfModelChanges, etc).

Then create your class:

public class MyDatabaseInitializer : MigrateDatabaseToLatestVersion
  <MyDbContext,
   MyDatabaseMigrationConfiguration>

Then also create the configuration for the initializer (ugh right?):

public  class DatabaseMigrationsConfiguration 
  : DbMigrationsConfiguration<MyDbContext>
{
    public DatabaseMigrationsConfiguration()
    {
        this.AutomaticMigrationDataLossAllowed = true;
        this.AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(MyDbContext context)
    {
        // Need data automagically added/update to the DB
        // during initialization?

        base.Seed(context);
    }
}

Then one way to initialize the database is:

var myContext = new MyDbContext(/*connectionString*/);

Database.SetInitializer<MyDbContext>(new MyDatabaseInitializer());
myContext.Database.Initialize(true);

Some people prefer the to use the command line to migrate databases, but I don't want to assume I'll always have access to the database from a command lin.

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • So, I have to give it an initializer even if I don't have values to seed? Basically, I need to create 2 classes _MyDatabaseInitializer_ and _MyDatabaseMigrationConfiguration_ (with no behaviour) so that Ef will create the context's tables? I mean, I need those classes for the EF to trigger table creation? – MikeSW Aug 04 '14 at 20:22
  • That is one way. There is the command line I referened, and there is the [Package Manager Console](http://msdn.microsoft.com/en-us/data/jj591621.aspx) way. In any case the real thing that has to happen is the call to `Context.Database.Initialize()` weither it's call by you, by command line, or package manager, something has to call it. – Erik Philips Aug 04 '14 at 20:24
  • Sadly, it doesn't work with migrations, because I'm using SQLite and it wants a SQlite migrator. Anything with drop database doesn't work either as it's not supported by the provider.... I think I'll just create the table 'manually' as in not with EF. Thanks for you help though. Who knew it's so convoluted to make EF to basically do a create table. – MikeSW Aug 04 '14 at 20:39
  • 1
    That is exactly why it is convoluted, because Microsoft doesn't create all the DAO providers to all the database servers. It's not always possible to know what is provided by external DAO providers. – Erik Philips Aug 04 '14 at 21:11
  • It sounds like a huge hassle. Code first... – Jeroen van Langen Mar 19 '22 at 12:58
  • @JeroenvanLangen this doesn't have anything to do with Code-First / Database-First. – Erik Philips Mar 21 '22 at 23:27