【问题标题】:unable to create entity framework database in class library无法在类库中创建实体框架数据库
【发布时间】:2014-09-16 14:21:28
【问题描述】:

我有一个类库。在这个项目中,我有一个名为“models”的文件夹,其中包含一些类模型。我有我需要的所有参考资料。

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>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="DefaultConnection2" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MDD;AttachDbFilename=|DataDirectory|\MDD.mdf;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

运行update-database 后会抛出以下错误:

发生文件激活错误。物理文件名“\MDD.mdf” 可能不正确。诊断并纠正其他错误,然后重试 操作。创建数据库失败。列出的一些文件名可能 不会被创建。检查相关错误。

【问题讨论】:

    标签: entity-framework entity-framework-migrations entity-framework-6.1


    【解决方案1】:

    解决了。

    app.config 不应该有&lt;connectionStrings&gt;Web.config 做。 然后在 MyDbContext.cs 中:

    public MyDbContext():base("connectionString"){}
    

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多