diff --git a/README.md b/README.md index c297a20..5cf4dc6 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,18 @@ You can use the SQLite CodeFirst in projects that target the following framework - .NET 4.6.1 (use net45) ## How to use +The functionality is exposed by using implementations of the `IDbInitializer<>` interface. +Depending on your need, you can choose from the following initializers: +- SqliteCreateDatabaseIfNotExists +- SqliteDropCreateDatabaseAlways +- SqliteDropCreateDatabaseWhenModelChanges + +If you want to have more control, you can use the `SqliteDatabaseCreator` (implements `IDatabaseCreator`) which lets you control the creation of the SQLite database. +Or for even more control, use the `SqliteSqlGenerator` (implements `ISqlGenerator`), which lets you generate the SQL code based on your `EdmModel`. + When you want to let the Entity Framework create database if it does not exist, just set `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` as your `IDbInitializer<>`. + +### Initializer Sample ```csharp public class MyDbContext : DbContext { @@ -56,9 +67,12 @@ public class MyDbContext : DbContext } } ``` +Notice that the `SqliteDropCreateDatabaseWhenModelChanges<>` initializer will create a additional table in your database. +This table is used to store some information to detect model changes. If you want to use a own entity/table you can implement the +`IHistory` interface and pass the type of your entity as parameter in the to the constructor from the initializer. In a more advanced scenario, you may want to populate some core- or test-data after the database was created. -To do this, inherit from `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` and override the `Seed(MyDbContext context)` function. +To do this, inherit from `SqliteDropCreateDatabaseAlways<>`, `SqliteCreateDatabaseIfNotExists<>` or `SqliteDropCreateDatabaseWhenModelChanges<>` and override the `Seed(MyDbContext context)` function. This function will be called in a transaction once the database was created. This function is only executed if a new database was successfully created. ```csharp public class MyDbContextInitializer : SqliteDropCreateDatabaseAlways @@ -73,6 +87,32 @@ public class MyDbContextInitializer : SqliteDropCreateDatabaseAlways - - + + + @@ -25,6 +26,7 @@ + \ No newline at end of file diff --git a/SQLite.CodeFirst.Console/Entity/CustomHistory.cs b/SQLite.CodeFirst.Console/Entity/CustomHistory.cs new file mode 100644 index 0000000..51ad92c --- /dev/null +++ b/SQLite.CodeFirst.Console/Entity/CustomHistory.cs @@ -0,0 +1,12 @@ +using System; + +namespace SQLite.CodeFirst.Console.Entity +{ + public class CustomHistory : IHistory + { + public int Id { get; set; } + public string Hash { get; set; } + public string Context { get; set; } + public DateTime CreateDate { get; set; } + } +} diff --git a/SQLite.CodeFirst.Console/FootballDbContext.cs b/SQLite.CodeFirst.Console/FootballDbContext.cs index 6f0ed9e..e3eabcf 100644 --- a/SQLite.CodeFirst.Console/FootballDbContext.cs +++ b/SQLite.CodeFirst.Console/FootballDbContext.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using System.Data.Entity; -using System.Data.Entity.ModelConfiguration.Conventions; +using System.Data.Entity; using SQLite.CodeFirst.Console.Entity; namespace SQLite.CodeFirst.Console @@ -16,8 +14,6 @@ public FootballDbContext() protected override void OnModelCreating(DbModelBuilder modelBuilder) { - modelBuilder.Conventions.Remove(); - ConfigureTeamEntity(modelBuilder); ConfigureStadionEntity(modelBuilder); ConfigureCoachEntity(modelBuilder); @@ -61,51 +57,4 @@ private static void ConfigurePlayerEntity(DbModelBuilder modelBuilder) .WillCascadeOnDelete(true); } } - - public class FootballDbInitializer : SqliteDropCreateDatabaseAlways - { - public FootballDbInitializer(DbModelBuilder modelBuilder) - : base(modelBuilder) - { } - - protected override void Seed(FootballDbContext context) - { - context.Set().Add(new Team - { - Name = "YB", - Coach = new Coach - { - City = "Zürich", - FirstName = "Masssaman", - LastName = "Nachn", - Street = "Testingstreet 844" - }, - Players = new List - { - new Player - { - City = "Bern", - FirstName = "Marco", - LastName = "Bürki", - Street = "Wunderstrasse 43", - Number = 12 - }, - new Player - { - City = "Berlin", - FirstName = "Alain", - LastName = "Rochat", - Street = "Wonderstreet 13", - Number = 14 - } - }, - Stadion = new Stadion - { - Name = "Stade de Suisse", - City = "Bern", - Street = "Papiermühlestrasse 71" - } - }); - } - } } diff --git a/SQLite.CodeFirst.Console/FootballDbInitializer.cs b/SQLite.CodeFirst.Console/FootballDbInitializer.cs new file mode 100644 index 0000000..8aa6199 --- /dev/null +++ b/SQLite.CodeFirst.Console/FootballDbInitializer.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using System.Data.Entity; +using SQLite.CodeFirst.Console.Entity; + +namespace SQLite.CodeFirst.Console +{ + public class FootballDbInitializer : SqliteDropCreateDatabaseWhenModelChanges + { + public FootballDbInitializer(DbModelBuilder modelBuilder) + : base(modelBuilder, typeof(CustomHistory)) + { } + + protected override void Seed(FootballDbContext context) + { + context.Set().Add(new Team + { + Name = "YB", + Coach = new Coach + { + City = "Zürich", + FirstName = "Masssaman", + LastName = "Nachn", + Street = "Testingstreet 844" + }, + Players = new List + { + new Player + { + City = "Bern", + FirstName = "Marco", + LastName = "Bürki", + Street = "Wunderstrasse 43", + Number = 12 + }, + new Player + { + City = "Berlin", + FirstName = "Alain", + LastName = "Rochat", + Street = "Wonderstreet 13", + Number = 14 + } + }, + Stadion = new Stadion + { + Name = "Stade de Suisse", + City = "Bern", + Street = "Papiermühlestrasse 71" + } + }); + } + } +} \ No newline at end of file diff --git a/SQLite.CodeFirst.Console/SQLite.CodeFirst.Console.csproj b/SQLite.CodeFirst.Console/SQLite.CodeFirst.Console.csproj index e91b289..c1379f4 100644 --- a/SQLite.CodeFirst.Console/SQLite.CodeFirst.Console.csproj +++ b/SQLite.CodeFirst.Console/SQLite.CodeFirst.Console.csproj @@ -53,15 +53,17 @@ - - ..\packages\System.Data.SQLite.Core.1.0.97.0\lib\net451\System.Data.SQLite.dll + + ..\packages\System.Data.SQLite.Core.1.0.99.0\lib\net451\System.Data.SQLite.dll True - - ..\packages\System.Data.SQLite.EF6.1.0.97.0\lib\net451\System.Data.SQLite.EF6.dll + + ..\packages\System.Data.SQLite.EF6.1.0.99.0\lib\net451\System.Data.SQLite.EF6.dll + True - - ..\packages\System.Data.SQLite.Linq.1.0.97.0\lib\net451\System.Data.SQLite.Linq.dll + + ..\packages\System.Data.SQLite.Linq.1.0.99.0\lib\net451\System.Data.SQLite.Linq.dll + True @@ -77,12 +79,14 @@ Properties\AssemblyVersionInfo.cs + + @@ -102,10 +106,10 @@ This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + - +