diff --git a/SQLite.CodeFirst.Console/Entity/Team.cs b/SQLite.CodeFirst.Console/Entity/Team.cs index 905be57..f8dcde0 100644 --- a/SQLite.CodeFirst.Console/Entity/Team.cs +++ b/SQLite.CodeFirst.Console/Entity/Team.cs @@ -12,6 +12,15 @@ public class Team : IEntity [Index("IX_Team_TeamsName")] // Test for named index. [Required] public string Name { get; set; } + public string Name2 { get; set; } + + [Column("NoName")] + [SqlDefaultValue(DefaultValue = "N")] + public string Name3 { get; set; } + + [Column("IntegerValue")] + [SqlDefaultValue(DefaultValue = "123")] + public int IntColumn { get; set; } public virtual Coach Coach { get; set; } diff --git a/SQLite.CodeFirst.Console/FootballDbInitializer.cs b/SQLite.CodeFirst.Console/FootballDbInitializer.cs index 59e9aa6..81f9914 100644 --- a/SQLite.CodeFirst.Console/FootballDbInitializer.cs +++ b/SQLite.CodeFirst.Console/FootballDbInitializer.cs @@ -1,17 +1,88 @@ using System.Data.Entity; -using SQLite.CodeFirst.Console.Entity; +using System.Data.Entity.Core.Metadata.Edm; +using System.Data.Entity.Infrastructure; +using System.IO; +using System.Linq; namespace SQLite.CodeFirst.Console { - public class FootballDbInitializer : SqliteDropCreateDatabaseWhenModelChanges + // changed the base class SqliteDropCreateDatabaseWhenModelChanges to SqliteCreateDatabaseIfNotExists + public class FootballDbInitializer : SqliteCreateDatabaseIfNotExists { + private const string DefaultValueMetadataName = + "http://schemas.microsoft.com/ado/2013/11/edm/customannotation:SqlDefaultValueAttribute"; public FootballDbInitializer(DbModelBuilder modelBuilder) - : base(modelBuilder, typeof(CustomHistory)) + : base(modelBuilder, true) { } protected override void Seed(FootballDbContext context) { // Here you can seed your core data if you have any. } + + public override void InitializeDatabase(FootballDbContext context) + { + string databseFilePath = GetDatabasePathFromContext(context); + + bool dbExists = File.Exists(databseFilePath); + if (dbExists) + { + var metadata = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace; + + var tables = metadata.GetItemCollection(DataSpace.SSpace) + .GetItems() + .Single() + .BaseEntitySets + .OfType() + .Where(s => !s.MetadataProperties.Contains("Type") + || s.MetadataProperties["Type"].ToString() == "Tables"); + + foreach (var table in tables) + { + var tableName = table.MetadataProperties.Contains("Table") + && table.MetadataProperties["Table"].Value != null + ? table.MetadataProperties["Table"].Value.ToString() + : table.Name; + + foreach (var member in table.ElementType.DeclaredMembers) + { + var cur = context.Database.SqlQuery($"PRAGMA table_info ({tableName})").ToList(); + if (cur.All(a => a.name != member.Name)) + { + if (member.MetadataProperties.Any(a => a.Name == DefaultValueMetadataName)) + { + var defaultValue = (SqlDefaultValueAttribute)member.MetadataProperties[DefaultValueMetadataName].Value; + context.Database.ExecuteSqlCommand( + $"ALTER TABLE {tableName} ADD COLUMN {member.Name} {GetTypeUsage(member)} DEFAULT '{defaultValue.DefaultValue}';"); + } + else + { + context.Database.ExecuteSqlCommand( + $"ALTER TABLE {tableName} ADD COLUMN {member.Name} {GetTypeUsage(member)} null;"); + } + + } + } + + } + } + + base.InitializeDatabase(context); + } + + private static string GetTypeUsage(EdmMember member) + { + return ((TypeUsage)((ReadOnlyMetadataCollection)member.MetadataProperties["MetadataProperties"].Value)["TypeUsage"].Value).EdmType.Name; + } + + private class TableInfo + { + public long cid { get; set; } + public string name { get; set; } + public string type { get; set; } + public long notnull { get; set; } + public string dflt_value { get; set; } + public long pk { get; set; } + } } } \ No newline at end of file diff --git a/SQLite.CodeFirst.Console/Program.cs b/SQLite.CodeFirst.Console/Program.cs index d3913c4..8b9bb2a 100644 --- a/SQLite.CodeFirst.Console/Program.cs +++ b/SQLite.CodeFirst.Console/Program.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; +using SQLite.CodeFirst.Console.Entity; +using System.Collections.Generic; using System.Data.Entity; using System.Data.SQLite; using System.Linq; -using SQLite.CodeFirst.Console.Entity; namespace SQLite.CodeFirst.Console { @@ -10,7 +10,7 @@ public static class Program { private static void Main() { - StartDemoUseInMemory(); + //StartDemoUseInMemory(); StartDemoUseFile(); PressEnterToExit(); }