2016-10-28 69 views
0

美好的一天,我正在使用實體框架6代碼首先從我的模型創建數據庫時,我的程序第一次運行。要做到這一點,我初始化我的模型上下文「的Global.asax.cs」但是,當過我運行程序我得到這個錯誤:實體框架6不創建數據庫

Format of the initialization string does not conform to specification starting at index 0.

請幫我傢伙。確實存在大量關於此錯誤的堆棧溢出問題,但是我已閱讀的所有帖子中,他們都需要web.config文件中的現有連接字符串。但在我的情況下,我沒有創建數據庫並嘗試連接到它。 我所擁有的是現有的模型,並期望EF 6在程序第一次運行時創建並初始化數據庫。

我在web.config中試過這個,但沒有結果。

<entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb"/> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 

這裏是我的Global.asax文件:

 public class MvcApplication : System.Web.HttpApplication 
     { 
      protected void Application_Start() 
      { 
       AreaRegistration.RegisterAllAreas(); 
       GlobalConfiguration.Configure(WebApiConfig.Register); 
       FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
       RouteConfig.RegisterRoutes(RouteTable.Routes); 
       BundleConfig.RegisterBundles(BundleTable.Bundles); 

      using (var context = new MultiTenantContext()) 
      { 
       var tenants = new List<Tenant>() 
       { 
        new Tenant() 
        { 
         Id = 1, 
         Name = "COG", 
         DomainName = "www.ChurchOfGod.com", 
         Default = true 
        }, 
        new Tenant() 
        { 
         Id = 1, 
         Name = "DIV", 
         DomainName = "www.developers.com", 
         Default = false 
        }, 
        new Tenant() 
        { 
         Id = 3, 
         Name = "CW", 
         DomainName = "www.carwash.com", 
         Default = false 
        }, 
       }; 
        context.Tenants.AddRange(tenants); 
        context.SaveChanges(); 
       } 
      } 
     } 

這裏是我的模型背景和模型類:

public class MultiTenantContext : DbContext 
    { 
     public DbSet<Tenant> Tenants { get; set; } 
    } 

    public class Tenant 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public string DomainName { get; set; } 
     public bool Default { get; set; } 
    } 

這裏是我的控制器:

public class TenantController : Controller 
    { 
     public ActionResult Index() 
     { 

      using (var context = new MultiTenantContext()) 
      { 
       var tenants = context.Tenants.ToList(); 
       return View(tenants); 
      } 

     } 
    } 
+2

你仍然需要一個連接字符串在你的配置,使EF知道哪裏來創建數據庫。 – Corporalis

回答

2

編輯您的模型到:

namespace WebApp.Models 
{ 
    public class MultiTenantContext : DbContext 
    { 
     public MultiTenantContext() : base("DB") 
     { 
     } 
     public DbSet<Tenant> Tenants { get; set; } 
    } 
} 

一下添加到web.config文件>的配置:

<connectionStrings> 
    <add name="DB" connectionString="data source=.\sqlExpress;initial catalog=XXX;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
+0

謝謝,但會在localdb中創建數據庫嗎? –

+0

你是否在連接字符串中看到localdb?它將在SQL Server Express中創建它。 – Amy

+0

謝謝@Kenneth。我實際上想要在localdb中創建數據庫,所以我只需將數據源改爲localdb即可。還有一件事我沒有改變我的模型,但我得到了它的工作。你能告訴我爲什麼我需要像你那樣改變我的模型嗎? –