5 Replies Latest reply on Aug 16, 2018 1:49 PM by Andrew Johnson

    C# Addin with Entity Framework 6 (EF6)

    Michael Corriveau-Cote

      Hey everyone,

       

      First timer here! I would normally post my questions on Stack Overflow but I believe that this place might be more optimal when it comes to questions about Solidworks and building a C# addin.

       

      I'm working right now on upgrading a VB.net Solidworks addin to a better version of it but in CSharp. I'm trying to migrate towards Entity Framework 6 when it comes to Database integration.

       

      So I started a Visual Studio 2017 SwAddin C# project. You can follow my steps in this post here. I then added the NuGet Package Entity Framework.

      When this is done, i'm adding an ADO.net Entity Data Model to my project.

       

      Adding ADO.Net Entity Data Model to project - Step 1 - Imgur.png

      Adding ADO.Net Entity Data Model to project - Step 2 - Imgur.png

      Adding ADO.Net Entity Data Model to project - Step 3 - Imgur.png

      Adding ADO.Net Entity Data Model to project - Step 4 - Imgur.png

      Then you may use a syntax like this one to Insert a new entry :

       

                  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();
      

       

      NOTE : I'd also like to know how to properly add a code snippet. (Answered)

       

      The problem is that this would work fine in any other projet. I tried a C# console project for example. But when it comes to trying this in an Addin, i'm getting the following error  :

      'No connection string named 'Entities' could be found in the application config file.'

       

      Solidworks Addin - EF6 Error - No connection string named 'Entities' could be found in the application config file - Imgur.png

       

      I tried many things like changing the App.config "Build Action" to Embedded Ressource, or changing the "Copy to Output Directory" property to Always Copy but nothing seems to work.

       

      The content is in my config file :

       

      Solidworks Addin - EF6 Error - Config File - Imgur.png

       

      Ce message a été modifié par : Michael Corriveau-Cote (Adding pictures)

       

      Ce message a été modifié par : Michael Corriveau-Cote (How to add code Snippet?)

        • Re: C# Addin with Entity Framework 6 (EF6)
          Jim Sculley

           

           

           

          NOTE : I'd also like to know how to properly add a code snippet.

           

          Click on  Advanced Editor at the upper right, select you snippet and the select Insert..Syntax Highlighting:

          • Re: C# Addin with Entity Framework 6 (EF6)
            Andrew Johnson

            Michael,

            You will need to manually assign the connection string to the DbContext in code, because by default it is looking for the connection string defined in the app.config file. Unfortunately it looks for the app.config of the application, not your Add-In dll. So, because SolidWorks is not a .NET application and has no app.config, there is no connection string to be found.

             

            DbContext has a constructor that takes the connection string as a parameter. Your context Info_IndusEntities will need a constructor to match. Because it is auto-generated by the .edmx, you will not want to modify it directly because changes may be overwritten if you modify the db schema and rebuild.

             

            Instead, you can add the partial keyword to Info_IndusEntities class, and in a separate file create a matching partial class that has this additional constructor. See code below:

             

            public partial class Info_IndusEntities
            {
                public Info_IndusEntities(string connectionString) : base(connectionString) { }
            }
            
            

             

            Now when you create an instance of your context, pass in the connection string.

              • Re: C# Addin with Entity Framework 6 (EF6)
                Michael Corriveau-Cote

                Andrew,

                 

                I tried what you just suggested and here are my results :

                I created a the Partial Class :

                 

                Partial.PNG

                I tried copying the Connection string from the Config file but it gave me an error.

                 

                  <add name="Info_IndusEntities" connectionString="metadata=res://*/Database.csdl|res://*/Database.ssdl|res://*/Database.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=trbsql02;initial catalog=Info_Indus;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

                Erreur1.png

                I tried then removing what was in error (which didn't make sense) but anyway :

                Erreur2.png

                 

                I also tried a custom connection string but didn't work either :

                 

                Info_IndusEntities context = new Info_IndusEntities("Data Source = trbsql02; Initial Catalog = Info_Indus; Integrated Security = True");

                erreur3.PNG

                  • Re: C# Addin with Entity Framework 6 (EF6)
                    Andrew Johnson

                    You need to use the connection string from the app.config, but replace &quot; with a standard double quote mark. &quot; is an XML token, only valid in an xml file.

                     

                    "metadata=res://*/Database.csdl|res://*/Database.ssdl|res://*/Database.msl;provider=System.Data.SqlClient;provider connection string=\"data source=trbsql02;initial catalog=Info_Indus;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework\""