美好的一天,我正在使用实体框架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);
}
}
}
你仍然需要一个连接字符串在你的配置,使EF知道哪里来创建数据库。 – Corporalis