带有 EF6 的 C# 插件 - 在应用程序配置文件中找不到名为 'Entities' 的连接字符串
C# addin with EF6 - No connection string named 'Entities' could be found in the application config file
我现在正在努力将 VB.net Solidworks 插件升级到更好的版本,但在 CSharp 中。在数据库集成方面,我正在尝试迁移到 Entity Framework 6。
所以我开始了一个 Visual Studio 2017 SwAddin C# 项目。您可以按照我在post中的步骤进行操作。然后我添加了 NuGet 包 Entity Framework.
完成后,我将 ADO.net 实体数据模型添加到我的项目中。
然后你可以使用像这样的语法来插入一个新条目:
Info_IndusEntities context = new Info_IndusEntities();
WillyDemandes newDemande = new WillyDemandes()
{
PathModel = "ModelTest",
ConfigName = "ConfigTest",
Revision = 1,
Priority = 2,
Statut = "EnTest",
WilmaTQ = true,
WilmaRBRE = true,
WilmaRBTK = true,
WilmaTLS = true,
GenerateBOM = true,
PdfPage = "L;",
ECO = "ECO #1234",
SendingComputer = System.Environment.MachineName,
Username = System.Environment.UserName,
MailAddress = "myemail@mycompany.com",
DateProduite = DateTime.Now
};
context.WillyDemandes.Add(newDemande);
context.SaveChanges();
问题是这在任何其他项目中都可以正常工作。例如,我尝试了一个 C# 控制台项目。但是当谈到在插件中尝试这个时,我收到以下错误:
'在应用程序配置文件中找不到名为 'Entities' 的连接字符串。'
我尝试了很多方法,例如将 App.config "Build Action" 更改为嵌入式资源,或将 "Copy to Output Directory" 属性 更改为始终复制,但似乎没有任何效果。
这是一个 Class 库项目,这是解决方案的启动项目。
内容在我的配置文件中:
解决这个问题
1- 将项目设置为启动项目
2- 确保在 entityFramework 部分之后添加连接字符串:
<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>
<connectionStrings>
<!-- your connection string goes here, after configSection -->
</connectionString>
不幸的是,Solidworks 加载插件 dll 的方式会阻止您(或您正在使用的库,例如 EF)以通常的方式访问 .config 文件。最简单的解决方案是为您的实体对象使用重载构造函数并从代码传递连接字符串。
我现在正在努力将 VB.net Solidworks 插件升级到更好的版本,但在 CSharp 中。在数据库集成方面,我正在尝试迁移到 Entity Framework 6。
所以我开始了一个 Visual Studio 2017 SwAddin C# 项目。您可以按照我在post
完成后,我将 ADO.net 实体数据模型添加到我的项目中。
然后你可以使用像这样的语法来插入一个新条目:
Info_IndusEntities context = new Info_IndusEntities();
WillyDemandes newDemande = new WillyDemandes()
{
PathModel = "ModelTest",
ConfigName = "ConfigTest",
Revision = 1,
Priority = 2,
Statut = "EnTest",
WilmaTQ = true,
WilmaRBRE = true,
WilmaRBTK = true,
WilmaTLS = true,
GenerateBOM = true,
PdfPage = "L;",
ECO = "ECO #1234",
SendingComputer = System.Environment.MachineName,
Username = System.Environment.UserName,
MailAddress = "myemail@mycompany.com",
DateProduite = DateTime.Now
};
context.WillyDemandes.Add(newDemande);
context.SaveChanges();
问题是这在任何其他项目中都可以正常工作。例如,我尝试了一个 C# 控制台项目。但是当谈到在插件中尝试这个时,我收到以下错误:
'在应用程序配置文件中找不到名为 'Entities' 的连接字符串。'
我尝试了很多方法,例如将 App.config "Build Action" 更改为嵌入式资源,或将 "Copy to Output Directory" 属性 更改为始终复制,但似乎没有任何效果。
这是一个 Class 库项目,这是解决方案的启动项目。
内容在我的配置文件中:
解决这个问题
1- 将项目设置为启动项目
2- 确保在 entityFramework 部分之后添加连接字符串:
<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>
<connectionStrings>
<!-- your connection string goes here, after configSection -->
</connectionString>
不幸的是,Solidworks 加载插件 dll 的方式会阻止您(或您正在使用的库,例如 EF)以通常的方式访问 .config 文件。最简单的解决方案是为您的实体对象使用重载构造函数并从代码传递连接字符串。