2014-03-31 41 views
0

似乎數據庫是用EF 6.1與6.0.2相比在意想不到的時間創建的。我需要一種方法來檢查數據庫是否被創建。如果它沒有創建,我創建它並運行一些種子代碼。我升級到6.1,這個邏輯不再起作用。EF 6.0.2和6.1之間的數據庫創建有所不同

我創建了一個簡單的項目,使用6.0.2,然後複製該項目,更新EF,你可以看到不同之處。

對於我首先使用代碼的兩個項目,創建遷移並運行解決方案。第一次應該看到數據庫還沒有被創建,並創建它並將其播下。問題是用於檢查數據庫是否被創建的代碼實際上在使用6.1時創建了數據庫。因此,檢查總是返回true,並且種子代碼永遠不會運行。

控制檯應用程式:

的Program.cs

namespace SimpleEFTest2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      MyContext.InitializeDatabase(); 
     } 
    } 
} 

TestEntity.cs:

namespace SimpleEFTest2 
{ 
    public class TestEntity 
    { 
     public int Id { get; set; } 
     public string SomeValue { get; set; } 
    } 
} 

MyContext.cs:

using System; 
using System.Data.Entity; 
using System.Data.Entity.Core.Objects; 
using System.Data.Entity.Infrastructure; 

namespace SimpleEFTest2 
{ 
    public class MyContext: DbContext 
    { 
     public DbSet<TestEntity> TestEntities { get; set; } 

     public ObjectContext ObjectContext 
     { 
      get 
      { 
       //With EF 6.0.2 this would throw an exception if the DB didn't exist. 
       //With EF 6.1.0 this creates the database, but doesn't seed the DB. 
       return ((IObjectContextAdapter)this).ObjectContext; 
      } 
     } 

     private static readonly Object syncObj = new Object(); 
     public static bool InitializeDatabase() 
     { 
      lock (syncObj) 
      { 
       using (var temp = new MyContext()) 
       { 
        ObjectContext oc = null; 
        try 
        { 
         oc = temp.ObjectContext; 
        } 
        catch (Exception ex) 
        { 
         //Ignore error 
         Console.WriteLine(ex); 
        } 
        //If oc != null && oc.DatabaseExists() return else create and seed DB 
        //With EF 6.1, oc.DatabaseExists() is always true because it was created in the function that gets the ObjectContext. 
        return true; 
       } 
      } 
     } 
    } 
} 

Configuration.cs

namespace SimpleEFTest2.Migrations 
{ 
    using System.Data.Entity.Migrations; 
    using System.Linq; 

    internal sealed class Configuration : DbMigrationsConfiguration<SimpleEFTest2.MyContext> 
    { 
     public Configuration() 
     { 
      AutomaticMigrationsEnabled = false; 
     } 

     protected override void Seed(SimpleEFTest2.MyContext myContext) 
     { 
      var entity = myContext.TestEntities.FirstOrDefault(i => i.SomeValue == "123"); 
      if (entity == null) 
      { 
       entity = new TestEntity { SomeValue = "123" }; 
       myContext.TestEntities.Add(entity); 
      } 
      myContext.SaveChanges(); 
     } 
    } 
} 

回答

1

我不知道爲什麼我沒有看到過這...

DbContext.Database有一個方法是否存在()。這將返回正確的結果而不創建數據庫。我更改了代碼以使用它,現在6.0.2和6.1.0的工作方式都是相同的。

新MyContext.cs:

using System; 
using System.Data.Entity; 
using System.Data.Entity.Core.Objects; 
using System.Data.Entity.Infrastructure; 
using SimpleEFTest2.Migrations; 

namespace SimpleEFTest2 
{ 
    public class MyContext: DbContext 
    { 
     public DbSet<TestEntity> TestEntities { get; set; } 

     public ObjectContext ObjectContext 
     { 
      get 
      { 
       //With EF 6.0.2 this would throw an exception if the DB didn't exist. 
       //With EF 6.1.0 this creates the database, but doesn't seed the DB. 
       return ((IObjectContextAdapter)this).ObjectContext; 
      } 
     } 

     private static readonly Object syncObj = new Object(); 
     public static bool InitializeDatabase() 
     { 
      lock (syncObj) 
      { 
       using (var temp = new MyContext()) 
       { 
        if (temp.Database.Exists()) return true; 

        var initializer = new MigrateDatabaseToLatestVersion<MyContext, Configuration>(); 
        Database.SetInitializer(initializer); 
        try 
        { 
         temp.Database.Initialize(true); 
         return true; 
        } 
        catch (Exception ex) 
        { 
         //Handle Error in some way 
         return false; 
        } 
       } 
      } 
     } 
    } 
} 
相關問題