lundi 27 avril 2015

Create SQLite database using Entity Framework

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.

Aucun commentaire:

Enregistrer un commentaire