带 EF6 的 C# 插件 - 在应用程序配置文件中找不到名为 'Entities' 的连接字符串

标签 c# database entity-framework-6 add-in solidworks

我现在正在努力将 VB.net Solidworks 插件升级到更好的版本,但在 CSharp 中。在数据库集成方面,我正在尝试迁移到 Entity Framework 6。

因此,我启动了一个 Visual Studio 2017 SwAddin C# 项目。您可以按照我在这篇文章中的步骤进行操作 here .然后我添加了 NuGet 包 Entity Framework 。

enter image description here

完成后,我将 ADO.net 实体数据模型添加到我的项目中。

enter image description here

enter image description here

enter image description here

enter image description here

然后你可以使用像这样的语法来插入一个新条目:

            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’的连接字符串。”

enter image description here

我尝试了很多方法,例如将 App.config 的“Build Action”更改为 Embedded Ressource,或将“Copy to Output Directory”属性更改为 Always Copy,但似乎没有任何效果。

这是一个类库项目,也是解决方案的启动项目。

内容在我的配置文件中:

enter image description here

最佳答案

不幸的是,Solidworks 加载插件 dll 的方式阻止您(或您使用的库,如 EF)以通常的方式访问 .config 文件。最简单的解决方案是为您的实体对象使用重载构造函数并从代码传递连接字符串。

关于带 EF6 的 C# 插件 - 在应用程序配置文件中找不到名为 'Entities' 的连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51877282/

相关文章:

c# - Serenity MVC 框架使用 Web 服务

c# - 内存中的字节与字节数组

database - 将代理键添加到现有 Oracle 数据库表的首选方法

mysql - 根据多个 id 对多个表中的列求和

database - 查找用于应用程序的数据库

c# - Entity Framework DbSet<TEntity>.Where(e => true) 性能

c# - Entity Framework 6 : audit/track changes

c# - 无法在 C# Windows 窗体中触发 MouseWheel 事件

c# - 使用 TransactionScope 在一个事务中使用多个数据库(多个 DbContext)

c# - WP7/Xaml 从中心旋转图像