EF6迁移在更新

时间:2015-10-27 21:39:59

标签: c# entity-framework migration entity-framework-6 ef-migrations

我正在通过迁移运行数据库更新,但EF正在创建一个名为my connection string的数据库,而不是使用数据库的名称创建。

正确的方法是使用 METIS 名称创建数据库,但创建为 MetisConnectionString

我做错了什么?

的app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionString>
    <add name="MetisConnectionString" connectionString="Server=.\sqlexpress;Database=METIS;User ID=metis_usr;Password=metis_usr;" providerName="System.Data.SqlClient" />
  </connectionString>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

DataContext.cs

public class MetisDataContext : DbContext
    {
        public MetisDataContext() 
            : base("MetisConnectionString")
        {
            Configuration.LazyLoadingEnabled = false;
            Configuration.ProxyCreationEnabled = false;
        }

        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new UserMapping());
        }
    }

1 个答案:

答案 0 :(得分:0)

您尚未向Entity Framework指定数据库的名称。因此,按照惯例,EF将使用您的连接字符串。您可以通过在

下面的连接字符串中指定来更改此设置
  <connectionStrings>
    <add name="MetisConnectionString" connectionString="Data Source=.\SQLExpress;Initial Catalog=YourDatabaseNameHere;User ID=metis_usr;Password=metis_usr;" providerName="System.Data.SqlClient" />
  </connectionStrings>

构造函数应如下所示

   public MetisDataContext() 
            : base("name=MetisConnectionString")
   {
            Configuration.LazyLoadingEnabled = false;
            Configuration.ProxyCreationEnabled = false;
   }