【问题标题】:Changing connection string after moving database to App_Data folder?将数据库移动到 App_Data 文件夹后更改连接字符串?
【发布时间】:2023-04-04 07:59:01
【问题描述】:

我正在尝试学习 mvc.net。我创建了一个使用模型优先方法的小项目。问题是我希望我的数据库显示在 App_data 文件夹中,因为我遵循了这篇文章:

http://www.dotnetfunda.com/articles/show/2182/how-to-embed-sql-database-in-appdata-folder

简而言之,我分离了数据库,然后将数据库包含在 app_data 文件夹中。现在我在更改连接字符串时遇到问题,以前我使用实体框架自动生成连接字符串,就像:

<add name="KeepitrememberEntities" 
connectionString="metadata=res://*/EDM.csdl|res://*/EDM.ssdl|res://*/EDM.msl;
provider=System.Data.SqlClient;provider connection string=&quot;
data source=mypcname\SQLEXPRESS;initial catalog=Keepitremember;
integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
providerName="System.Data.EntityClient" />

现在我把它改成了:

<add name="KeepitrememberEntities"
connectionString="Data Source=mypcname\SQLEXPRESS;
AttachDbFilename=|DataDirectory|Keepitremember.mdf;Integrated Security=True;
User Instance=True"
providerName="System.Data.SqlClient" />

当我执行代码并尝试使用表单保存值时,它向我显示 EDM.Context.cs 中的错误并且错误是:

类型异常 'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException' 发生在 Emptymvc.dll 中但未在用户代码中处理

附加信息:使用 Database First 的 T4 模板生成的代码 如果在代码中使用 Model First 开发可能无法正常工作 第一种模式。要继续使用数据库优先或模型优先,请确保 在配置中指定了实体框架连接字符串 执行应用程序的文件。要使用这些类,那是 从 Database First 或 Model First 生成,使用 Code First 添加任何 使用属性或 DbModelBuilder API 的附加配置 然后删除引发此异常的代码。

我现在需要做什么,任何解决方案!

感谢您的宝贵时间。

【问题讨论】:

    标签: c# sql-server entity-framework asp.net-mvc-4 web-config


    【解决方案1】:

    据我所知,第一个连接字符串是实体框架连接字符串,第二个是 SQL Express 连接字符串。在 EF 项目中,它们都应该存在于 web.config 文件中以供 DbContext 和代码生成模板使用 - 因此它们不可互换(即,您不应将 EF 连接字符串更改为 SQL Express 字符串,反之亦然)。

    由于您已将包含 EF 所需的 CSDL、SSDL 和 MSL 信息的 EF 连接字符串更改为 SQL Express 连接字符串,因此 EF 假定现有数据库元数据不再存在并尝试像 Code First 那样创建新数据库,因此它执行OnModelCreating方法后触发UnintentionalCodeFirstException

    无需更改web.config 中的EF 连接字符串,只需在与此相同的connectionStrings 元素上添加 SQL Express 连接字符串(您可能需要在需要时定义初始目录名称):

    <connectionStrings>
        <!-- SQL Express connection string -->
        <add name="KeepitrememberConnection"
        connectionString="Data Source=mypcname\SQLEXPRESS;Initial Catalog=Keepitremember;
        AttachDbFilename=|DataDirectory|Keepitremember.mdf;Integrated Security=True;
        User Instance=True"
        providerName="System.Data.SqlClient" />
    
        <!-- EF connection string -->
        <add name="KeepitrememberEntities" 
        connectionString="metadata=res://*/EDM.csdl|res://*/EDM.ssdl|res://*/EDM.msl;
        provider=System.Data.SqlClient;provider connection string=&quot;
        data source=mypcname\SQLEXPRESS;initial catalog=Keepitremember;
        integrated security=True;MultipleActiveResultSets=True App=EntityFramework&quot;" 
        providerName="System.Data.EntityClient" />
    </connectionStrings>
    

    注意:连接字符串名称应与 EF 模型生成架构中的预定义名称相同

    相关问题:

    Model First with DbContext, Fails to initialize new DataBase

    Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode

    Entity Framework cant use the DbContext, model being created

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多