简体   繁体   English

实体框架6未创建数据库

[英]Entity Framework 6 not creating database

Good day guys, I am using Entity framework 6 Code First to create a database from my model when my program first runs. 大家好,我是在第一次运行程序时使用Entity Framework 6 Code First从模型创建数据库的。 To do this I initialize my model context in "Global.asax.cs" But when ever I run the program I get this error: 为此,我在“ Global.asax.cs”中初始化我的模型上下文,但是每当我运行该程序时,都会出现此错误:

Format of the initialization string does not conform to specification starting at index 0. 初始化字符串的格式不符合从索引0开始的规范。

Please help me guys. 请帮助我。 It is true that there tons of post on stack overflow concerning this error, but of all the posts that I have read they all need an existing connection string in web.config file. 确实,与此错误有关的堆栈上有大量帖子溢出,但是在我阅读的所有帖子中,它们都需要web.config文件中的现有连接字符串。 But in my case I don't have a database created already and trying to connect to it. 但就我而言,我还没有创建数据库并尝试连接它。 What I do have is a model existing and expecting EF 6 to create a and initialize the database when the program first run. 我所拥有的是一个现有的模型,期望EF 6在程序首次运行时创建一个并初始化数据库。

I have tried this in web.config, but no result. 我已经在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>

Here is my Global.asax file: 这是我的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();
              }
           }
        }

Here is my model context and model class: 这是我的模型上下文和模型类:

    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; }
    }

Here is my controller: 这是我的控制器:

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

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

        }
    }

Edit your model to: 将模型编辑为:

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

Add this to web.config > configuration: 将此添加到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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM