1

I am trying to create my first database. I see countless posts on creation being done using existing database and a config file. What I am trying to achieve though, is to create database from code and also do initialization inside code behind, without a config file.

I have the following inside my MyContext class:

   public MyContext() : base("MyDB") 
{
   DatabaseConfiguration _DBConfig = new DatabaseConfiguration();
}

    public DbSet<MyObject> MyObject { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions
            .Remove<PluralizingTableNameConvention>();
    }

Assuming that whenever I create an instance of MyContext, I should get a Database created if such does not exist. But no file gets created.

Initialization is done as following:

class DatabaseConfiguration : DbConfiguration
{
    public DatabaseConfiguration()
        {
            SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
            SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
            SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
        }
}

I am using newest versions of SQLite and EF6 from NuGet.

3
  • 1
    If I recall correctly, the database is not actually created until you need it. I.e. populate it. Commented Apr 27, 2015 at 19:28
  • How are you telling your context to use DatabaseConfiguration? Simply declaring an local variable in the constructor won't do it. Commented Apr 27, 2015 at 19:28
  • @mason look closer into what is happening inside the constructor, those are explicit provider settings being executed. Commented Apr 28, 2015 at 17:33

2 Answers 2

2

You think you are telling it to use your DatabaseConfiguration, but you're not. Simply declaring a local variable in your constructor doesn't tell the context to use that configuration. You can tell it with the DbConfigurationType attribute.

[DbConfigurationType(typeof(DatabaseConfiguration))] 
public class MyContext : DbContext
{
    public MyContext() : base("MyDB") 
    {
    }

    public DbSet<MyObject> MyObject { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

So far as I know, Entity Framework doesn't support creating database + tables for SQLite. You can create a database template by external tool then run manual migrations as suggested in this post for creating or updating database schema.

1 Comment

Unfortunately, given link is not valid anymore

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.