none
Problem using ADO.NET Entity Framework from a DLL class-library : RRS feed

  • Question

  • I am trying to separate the business logic from my application (wpf) to a DLL class library but when I am trying to instantiate the data-model I get the following exception (Entities.Designer.vb):





    "The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid."

    Note: the connection string is properly set in the DLL's app.config file.

    Thanks!
    Shimmy
    Thursday, April 2, 2009 2:22 PM

Answers

  • And if you are using Beta 3 try with this code:
    public static string GetConnection(string libraryName, string folderName, string modelName, string connectionString)
            {
                string strRes = string.Empty;
                
                if(!string.IsNullOrEmpty(folderName))
                    strRes="res://" + libraryName + "/" + folderName + ".";
    
                else
                    strRes = "res://" + libraryName + "/";
    
                EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
    
                entityBuilder.ProviderConnectionString = connectionString;
    
                entityBuilder.Metadata = strRes + modelName + ".csdl|" + strRes + modelName + ".ssdl|" + strRes + modelName + ".msl";
    
                entityBuilder.Provider = "System.Data.SqlClient";
    
                return entityBuilder.ToString();
            }


    Friday, April 3, 2009 3:01 PM
  • Yes the connection string is created by the entity model designer in the respective project. In this scenario you will have to copy the connectionstring from the entity project and paste it inside the UI Project's app.config file under the section <connectionStrings>  like below 

    <connectionStrings>
      <add name="SchoolEntities" connectionString="metadata=res://*/SchoolModel.csdl|res://*/SchoolModel.ssdl|res://*/SchoolModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\yukon;Initial Catalog=School;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>



    Bindesh V[MSFT]-SQL Developer Support -------------------------------------- This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, September 4, 2009 2:06 AM
  • OK I found it:

    here is the valid connection string:

    metadata=res://*/Data.Model.csdl|res://*/Data.Model.ssdl|res://*/Data.Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=PC\SQLEXPRESS;Initial Catalog=DB;Integrated Security=True"

    When I try to use the same connection string from app.config instantiating the context by New() I get the error.
    Note that if I instantiate it in a console application it does work; the problem only occurs when using in a DLL.

    would like to hear more.


    Shimmy
    Saturday, April 11, 2009 10:18 PM
  • Problem solved... finally. Have to say though... this is pretty dumb.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/70f124b6-5003-4011-8142-9c7e4ac6c4a0/

    Basically, the ConnectionString must be named after the ObjectContext to which is represents.

    Also, i read some of the guys use a single quote instead of
    &quot; in the app.config file's connection string.

    So this:

    < add name = "SchoolDataEntities " connectionString = "metadata=res://*;provider=System.Data.SqlClient;provider connection string= &quot; Data Source=.\SQLEXPRESS;Initial Catalog=SchoolData;Integrated Security=True;MultipleActiveResultSets=True &quot; " providerName = "System.Data.EntityClient " />

    Changes to this:

    < add name = "SchoolDataEntities " connectionString = "metadata=res://*;provider=System.Data.SqlClient;provider connection string= ' Data Source=.\SQLEXPRESS;Initial Catalog=SchoolData;Integrated Security=True;MultipleActiveResultSets=True ' " providerName = "System.Data.EntityClient " />


    AND... you need to have the app.config file included in your Service layer's project as well. The reason for this is explained at the end of the topic in the link I posted above.

    Cheers

    "Murphy's law??? Who is this Murphy? And when did he come in the office and made my hardrive crash?"
    Thursday, April 16, 2009 2:51 PM

All replies

  • Hi,
    you need to copy the EntityConnection string into the App.Config of your wpf application.
    Thursday, April 2, 2009 3:38 PM
  • It's done, no results.

    Note: The context is instantiated in the DLL, not in the application, the application only accesses the functionality.
    Shimmy
    Thursday, April 2, 2009 3:42 PM
  • Under connectionStrings like this ?

     

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <connectionStrings>
    <add name="EntityContext" connectionString="metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=LONS131D31\SKY2005_DEV_01;Initial Catalog=SKY2009R1;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
    </configuration>

     

     

    Thursday, April 2, 2009 3:49 PM
  • Yes I did but I still get the same error as in the picture.
    Shimmy
    Thursday, April 2, 2009 3:50 PM
  • Shimmy,

    Are you using Beta 3 or v1? That is a problem from Beta 3 and it should be fixed in v1. Anyhow try with this code:

    public static EntityConnection GetEntityConnection(string connectionString, Assembly executingAssembly)
            {
                SqlConnection sqlConnection = new SqlConnection(connectionString);
    
                MetadataWorkspace workspace = new MetadataWorkspace(new string[] { "res://*/" }, new Assembly[] { executingAssembly });
    
                return new EntityConnection(workspace, sqlConnection);
            }
    Friday, April 3, 2009 2:59 PM
  • And if you are using Beta 3 try with this code:
    public static string GetConnection(string libraryName, string folderName, string modelName, string connectionString)
            {
                string strRes = string.Empty;
                
                if(!string.IsNullOrEmpty(folderName))
                    strRes="res://" + libraryName + "/" + folderName + ".";
    
                else
                    strRes = "res://" + libraryName + "/";
    
                EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
    
                entityBuilder.ProviderConnectionString = connectionString;
    
                entityBuilder.Metadata = strRes + modelName + ".csdl|" + strRes + modelName + ".ssdl|" + strRes + modelName + ".msl";
    
                entityBuilder.Provider = "System.Data.SqlClient";
    
                return entityBuilder.ToString();
            }


    Friday, April 3, 2009 3:01 PM
  • Hi Federico!

    I am using VS 2008 SP1, I donno what version of EF it actually is.

    However, I didn't understand how and where to actually use your functions at all.
    please be more specific.

    Thanks
    Saturday, April 4, 2009 6:21 PM
  • OK, I played around with your function and gt the point. now I get the following exception:
    Shimmy
    Saturday, April 4, 2009 10:01 PM
  • Shimmy,

    for example like this:

    1.connectionString is your connectionString
    ModelEntities context=new ModelEntity(connectionString, Assembly.GetExecutingAssembly());

    or like this:
    1. The model is inside a folder called Models
    2. The name of the library is ClassLibrary1
    3. The name of the EF model is ClassLibrary1Model
    4. connectionString is your connectionString

    If you put the model lets in a folder called Model for example:
    ModelEntities context=new ModelEntity(GetConnection("ClassLibrary1", "Models", "ClassLibrary1Model", connectionString));

    I hope this helps
    Saturday, April 4, 2009 11:49 PM
  • Ok, I got it already, still stuck with previous message (MetadataException).
    Shimmy
    Sunday, April 5, 2009 12:13 AM
  • Hi Shimmy,

    Did you manage to solve your problem?

    It sound like your edmx outputs aren't saved in the way the connection string thinks.

    There are 2 ways to define edmx:
    1. output to folder - creates csdl, msl & ssdl in the output folder (bin\debug)
    2. output to assembly - creates the files and saves them in the assembly

    In the second option, your connection string should contain "res" uri, in the first option, the connection string doesn't use the "res" uri.

    Find out how your edmx is configured and apply the correct connection string.

    Look at the following liks:
    http://msdn.microsoft.com/en-us/library/bb896291.aspx - first option
    http://msdn.microsoft.com/en-us/library/bb738533.aspx - second option

    If you need any help, please specify which option you have in the edmx and show us your connection string.
    Tuesday, April 7, 2009 7:56 AM
  • Hello Ido and thanks for your reply,

    I didn't manage to solve my problem yet.

    I also didn't find a way to examine how my files are configured.

    I hope the following details will be helpful:

    1. The edmx file exsists in <Assembly Name>\Data\Model.edmx
    2. I checked in bin\Debug; none of the model files are there (there is a folder Data but it's empty)
    3. Here is the connection string:
      <
      add name="Entities" connectionString="metadata=res://*/Data.Model.csdl|res://*/Data.Model.ssdl|res://*/Data.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=PC\SQLExpress;Initial Catalog=DB;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

    I really hope there is a way to do that.
    תודה רבה


    Shimmy
    Saturday, April 11, 2009 9:37 PM
  • FYI:

    I tried creating a new solution that consists of a class library and a console app, I added the edmx (generated anew) to the root of the project and it doesn't work,
    I tried changing res://*/ in the connection string to .\ and it still doesn't work.
    Shimmy
    Saturday, April 11, 2009 9:54 PM
  • OK I found it:

    here is the valid connection string:

    metadata=res://*/Data.Model.csdl|res://*/Data.Model.ssdl|res://*/Data.Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=PC\SQLEXPRESS;Initial Catalog=DB;Integrated Security=True"

    When I try to use the same connection string from app.config instantiating the context by New() I get the error.
    Note that if I instantiate it in a console application it does work; the problem only occurs when using in a DLL.

    would like to hear more.


    Shimmy
    Saturday, April 11, 2009 10:18 PM
  • Hi Shimmy -

    A copule of things could be happening here. 

    1.  You may not be loading the assembly that contains your embedded csdl/ssdl/msl resources
    2.  the assembly may not have the embedded csdl/ssdl/msl resources
    3.  the assembly may not have the embedded csdl/ssdl/msl resources with the name you are expecting.

    To verify, do the following:

    To verify 1, look at the "Debug|Modules" window in visual studio when you have a debugger attached to your app, and note the full path for the assembly that contains your edmx resources.

    To verify 2, open the assembly you identified in 1 above in .net reflector and look at the resources.   You should see the embedded csdl, ssdl & msl files.

    To verify 3,  check the names of the embedded resources and verify that they match with the names you are using in your connection string.

    Thanks,

    Mike Kaufman
    Microsoft Corp.
    This posting is provided "AS IS" with no warranties, and confers no rights
    Tuesday, April 14, 2009 9:38 PM
    Moderator
  • Hi Shimmy,

    it's possible that your class library is not loaded if you are not "using" any classes from your library.

    CLR won't load an object unless it is needed. If your class library only contains embedded resources, it won't be loaded even if your application holds a reference to it.

    How about having ObjectContext in the class library and instantiate the context in your application? If it still doesn't work, could you please send your project to jichang at microsoft.com  and I will take a look. 

    Thanks,

    Jing Chang - MSFT -------------------------------------------------------------------------------- This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, April 15, 2009 1:21 AM
    Moderator
  • I started working with LINQ to EF beginning of the year in my spare time. Ran into a problem almost straight away. I could not figure out how in the world I can query from 3 Tables/Entities and then today out of the blue a person respons to one of my posts with a solution. Brilliant I thought.

    Now I have the exact same problem mentioned in the first post, although I'm working in C#.

    I used this as an example to work from.
    http://www.c-sharpcorner.com/UploadFile/ankithakur/Getting_started_with_ADO_NET_Entity_Framework09302008085544AM/Getting_started_with_ADO_NET_Entity_Framework.aspx

    The example works perfectly. I then went and made a new project and basically put my Entity model in it's own dll. Now I get the error as shown in the first post.

    I REALLY want to learn LINQ to EF, but it's so frustrating most of the time. It's like Linux. When it works, it's brilliant. As soon as something is wrong though, you need to google yourself to death to try and find answers and then most of the results are people with the same problem but with no solution.

    After 3 months of getting answers that end up not working anyway, I've decided to scrap LINQ 2 EF completely... finish my app that basically creates my own Entity model and stick to SQL queries.

    I think LINQ is worse than Clarion (Not using Templates)...


    Sorry to rant in your thread Shimmy, but it's basically the most relavent post I found regarding this.

    "Murphy's law??? Who is this Murphy? And when did he come in the office and made my hardrive crash?"
    Wednesday, April 15, 2009 2:25 PM
  • NeCroFire,
    Please accept my appreciation for your ranting, if I didn't do I am happy that there are at least those who do, I really hope in one day Microsoft to really understand things about development...


    Shimmy
    Wednesday, April 15, 2009 8:05 PM
  • Problem solved... finally. Have to say though... this is pretty dumb.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/70f124b6-5003-4011-8142-9c7e4ac6c4a0/

    Basically, the ConnectionString must be named after the ObjectContext to which is represents.

    Also, i read some of the guys use a single quote instead of
    &quot; in the app.config file's connection string.

    So this:

    < add name = "SchoolDataEntities " connectionString = "metadata=res://*;provider=System.Data.SqlClient;provider connection string= &quot; Data Source=.\SQLEXPRESS;Initial Catalog=SchoolData;Integrated Security=True;MultipleActiveResultSets=True &quot; " providerName = "System.Data.EntityClient " />

    Changes to this:

    < add name = "SchoolDataEntities " connectionString = "metadata=res://*;provider=System.Data.SqlClient;provider connection string= ' Data Source=.\SQLEXPRESS;Initial Catalog=SchoolData;Integrated Security=True;MultipleActiveResultSets=True ' " providerName = "System.Data.EntityClient " />


    AND... you need to have the app.config file included in your Service layer's project as well. The reason for this is explained at the end of the topic in the link I posted above.

    Cheers

    "Murphy's law??? Who is this Murphy? And when did he come in the office and made my hardrive crash?"
    Thursday, April 16, 2009 2:51 PM
  • One thing to consider is that the context class has a number of overloads and different ways for you to give the connection string and metadata to the EF runtime.  There will certainly be scenarios when having a connection with the same name as your EntityContainer isn't feasible or desirable.
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, May 1, 2009 9:16 PM
  • Consider that the hosting application ought to have the connection string.

    For example, in my work, I use either a Web Site project or a Web Service project.

    Business Logic (the EDMX et al) goes in a custom Class Library project.

    For unit testing and EDMX Designer support, the App.config in the custom Class Library project has a valid connection string.

    However, at run-time, the Business Logic selects the connection from the hosting application-- the root web.config in the Web Site project or the Web Service project.

    Alternately, one could pass the connection string in from the host to the BusinessLayer, but that's a security risk when the host is a Web Service.

    Admittedly, purists would argue that using HttpContext.Current in the BusinessLayer is a no-no; but, that's something I can live with for this one exceptional case.

    I even dynamically select a debug-connection-string or a non-debug-connection-string at run-time, for running in a test environment.

    Occam. Easy. Fast. Simple.

    The one downside is the propogation of the connection string into the Class Library, but that it just for testing and designer support, not production.

    (I cannot use Beta projects so I have the release of EF that came with Framework Version 3.5 SP1.)

    (I grant that I may not fully understand your solution. If that is the case, then please ignore my post and accept my apology.)

    HTH.

    Thank you.

    -- Mark Kamoski
    mkamoski
    Wednesday, June 10, 2009 4:23 PM
  • Hi all,
    I have 1 solution with two projects. They are : UI project and Entity project.
    I have button and datagrid in UI project. when I click the button, the datagrid will be fill with the data I get from Entity Project
    when I put the EntityFramework and appConfig on the UI project, it's working fine.
    But, my problem is :
    When I put the EntityFramework ans appConfig on the Entity Project, it always return an error same like this first post.

    here is my connection string.
    <add name="PublishingCompanyEntities"
             connectionString="metadata=res://*;
             provider=System.Data.SqlClient;
             provider connection string='Data Source=.\SQLSERVER2005;
             Initial Catalog=PublishingCompany;User ID=sa;Password=sa;Integrated Security=True;
             MultipleActiveResultSets=True'"
             providerName="System.Data.EntityClient" />

    could anyone explain why and hoe to solved?

    thanks in advance.
    Vera
    Thursday, September 3, 2009 1:37 AM
  • Hi Vera

    Did you mean that you deleted the connection string in UI Project and added it inside the Entity project ?
    In general, you should put the entity connection string in the project where you reference the entity model assembly. In the above case you should put the connection string inside hte app.config for the UI Project since the dll for entity will load in that context.


    Thanks
    Bindesh V[MSFT]-SQL Developer Support
    Thursday, September 3, 2009 4:32 AM
  • Hi Bindesh

    Sorry, but i think I miss on giving an information. Here is the detail.
    First, I made 1 solution with one project.
    On the project, I have
    Window1.xaml,
    Window1.xaml.cs
    Company.edmx
    app.config (with the connection string in it)
    Mapper.cs

    Here is the code in Mapper.cs
    public List<Author> GetAllAuthors()
    {
       CompanyEntities pb = new CompanyEntities();
       List<Author> authorList = new List<Author>();
       authorList = pb.Authors.ToList<Author>();
       return authorList;
     }

    Here is the code in code behind(Window1.xaml.cs)
    private void btnKik_Click(object sender, RoutedEventArgs e)
    {
       Mapper map = new Mapper();
       grid.ItemsSource = map.GetAllAuthors();
    }

    This code is run well.

    Second, I made 1 solution with two projects. They are UIProject and EntityProject.
    UIProject only have :
    Window1.xaml,
    Window1.xaml.cs

    and then I create the EntityFramework in EntityProject.
    so, the Company.edmx and app.config (with the connection string in it) will be inside this EntityProject.
    And then, I create the Mapper.cs in EntityProject.

    I add EntityProject and System.Data.Entity as references in UIProject.
    I use the same code inside the Window1.xaml.cs and Mapper.cs.
    But it always return an error same like this first post.
    Where should I put the app.Config? because its automatically generated when I create the edmx files.
    ah, I use Microsoft Visual Studio Beta1. Microsoft .Net Framework Version 4.0.

    Thanks in advance

    Regards,
    Vera


    Friday, September 4, 2009 1:59 AM
  • Yes the connection string is created by the entity model designer in the respective project. In this scenario you will have to copy the connectionstring from the entity project and paste it inside the UI Project's app.config file under the section <connectionStrings>  like below 

    <connectionStrings>
      <add name="SchoolEntities" connectionString="metadata=res://*/SchoolModel.csdl|res://*/SchoolModel.ssdl|res://*/SchoolModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\yukon;Initial Catalog=School;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>



    Bindesh V[MSFT]-SQL Developer Support -------------------------------------- This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, September 4, 2009 2:06 AM
  • Hi Bindesh.
    Yes its working now. Thanks for your help.
    Firstly, I think the connection string use will refer to connection string created by the entity model designer.
    Thanks again :)

    Regards,
    Vera
    Friday, September 4, 2009 2:28 AM
  • I got the same Problem. Does Anybody know if this was fixed? We have 2011 -,-
    Tuesday, October 4, 2011 1:49 PM
  • I got the same Problem. The fix is as below,

    1. Add the connect string to the current project app.config. And don't forget to change &quot; to '.

    2. Install EntityFramework in the current project by Manage NuGet Packages.

    Wednesday, June 17, 2015 9:02 PM
  • Thank you so much. I forgot about the second
    Monday, April 16, 2018 12:21 PM