简体   繁体   English

实体框架生成的上下文使用错误的连接字符串

[英]Entity Framework generated Context uses wrong connection string

I added an ADO.NET Entity Data Model to my project, which created a connection string in app.config looking like this: 我向项目中添加了一个ADO.NET实体数据模型,该模型在app.config中创建了一个连接字符串,如下所示:

<add name="KeyTranslatorEntities" connectionString="metadata=res://*/DAL.Model.csdl|res://*/DAL.Model.ssdl|res://*/DAL.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=ZZZZZ.database.windows.net,1433;initial catalog=KeyTranslator;persist security info=True;user id=YYYYY;password=XXXXX;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

It also generated the class KeyTranslatorEntities which inherits from DbContext and has this default constructor: 它还生成了类KeyTranslatorEntities,该类继承自DbContext并具有以下默认构造函数:

public KeyTranslatorEntities()
        : base("name=KeyTranslatorEntities")
    {
    }

I create a KeyTranslatorEntities using the default generated constructor but upon inspection, the instance uses the connection string: 我使用默认生成的构造函数创建了一个KeyTranslatorEntities,但是在检查后,该实例使用了连接字符串:

"data source=localhost;initial catalog=KeyTranslator;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"

Why is it not using the correct connection string? 为什么没有使用正确的连接字符串?

The problem was that the solution consisted of two different projects. 问题在于该解决方案由两个不同的项目组成。 One containing the EntityFramework code and another which was a WebService project with a dependency on the other project. 一个包含EntityFramework代码,另一个包含一个WebService项目,该项目依赖于另一个项目。 The WebService project contained the first connection string which was being used. WebService项目包含正在使用的第一个连接字符串。

Lesson learned: Make sure the startup project's config files are updated. 获得的经验教训:确保已更新启动项目的配置文件。

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

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