2

I have a C# ASP.NET MVC code first project that works as intended to connect to my database. It has the connection string set in the web.config file and all seems to work great.

<connectionStrings>
    <add name="HorstMFGContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|HorstMFG.mdf;Initial Catalog=aspnet-HorstMFG;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

Now I need to connect to the same database from a different application that needs to be a WinForms based class library. I don't have access to the source code of the program that uses my dll because my application is just a plug-in for Autodesk Vault.

There are numerous examples such as this one that show how to set the connection string in the application that calls the dll, but that obviously won't work in my case.

This link here seems to very close to what I need, but I haven't been able to get it to work. Here is my version of the 'Create' function.

public static HorstMFGEntities Create(string nameOrConnectionString)
        {
            var entityBuilder = new EntityConnectionStringBuilder();

            // use your ADO.NET connection string
            entityBuilder.ProviderConnectionString = nameOrConnectionString;

            // Set the Metadata location.
            entityBuilder.Metadata = @"res://*/HorstMFG.ssdl|res://*/HorstMFG.msl";


            return new HorstMFGEntities(entityBuilder.ConnectionString);
        }

Here is the line that calls the function:

using (var db  = HorstMFGEntities.Create(@"data source=(localdb)\MSSQLLocalDB;attachdbfilename=C:\Users\lorne\source\repos\HorstMFG\HorstMFG\App_Data\HorstMFG.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"))
                {
                    foreach (string l in lineList3)
                    {
                       ....
                       ....

Here is the actual line that throws the exception.

// calculate material
                        Material mat = db.Materials.Where(m => m.StructuralCode == l.Split('\t')[6]).FirstOrDefault();

The exception message "Error: Keyword not supported: 'metadata'.

Any help to point me in the right direction is appreciated. Thanks.

I updated my code as per the first comment, and also added the 'provider' that I had missed as well.

public static HorstMFGEntities Create(string nameOrConnectionString)
        {
            var entityBuilder = new EntityConnectionStringBuilder();

            // use your ADO.NET connection string
            entityBuilder.ProviderConnectionString = nameOrConnectionString;

            entityBuilder.Provider = "System.Data.SqlClient";

            // Set the Metadata location.
            entityBuilder.Metadata = @"res://*/HorstMFG.csdl|res://*/HorstMFG.ssdl|res://*/HorstMFG.msl";


            return new HorstMFGEntities(entityBuilder.ConnectionString);
        }

Now I get the error "Error: Unable to load the specified resource."

I now followed the instructions in the link you provided. I think I must be getting close, but I'm a little foggy on the metadata format yet. There seems to be a portion commented out in the example. I replaced it with this:

entityBuilder.Metadata = "res://*/HorstMFG.csdl|res://*/HorstMFG.ssdl|res://*/HorstMFG.msl";

Now I get the error: "Unable to load the specified metadata resource"

I confirmed that the files I am referencing do exist, they are in the ...\obj\Debug\edmxResourcesToEmbed\ folder in my project. I also changed the 'build action' for the 'HorstMFG.edmx' object from 'None' to 'Embedded Resource'. That didn't help anything.

enter image description here

Lorne
  • 79
  • 1
  • 9
  • I used this connection builder class a few years ago and I know it works...did notice a difference in your example and the one you linked to. Your version is missing the "Model.csdl" in the meta...pretty sure EF requires all 3 res links. Try it like this and see if it works. res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl – user1011627 Jun 07 '18 at 14:22
  • 1
    EF code first needs none of them. – Reza Aghaei Jun 07 '18 at 14:43
  • See additional info in edited question. – Lorne Jun 07 '18 at 15:00
  • Sorry, I glossed over the fact you were doing code first...take a look at this SO question...it's more inline with what you are trying to do. https://stackoverflow.com/questions/25092218/dynamic-connection-string-for-ef-code-first – user1011627 Jun 07 '18 at 15:06
  • Added more details to original question. – Lorne Jun 07 '18 at 16:18

0 Answers0