0

I've seen a lot of solutions for what appear to be similar problems, but nothing that seems to resolve my issues. I am having a MetadataException thrown when I try to execute Linq queries on Entities.

using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity.Core;

namespace MyCompany.AppName.Entity
{
    public class ModelReader
    {
        public static MyEntities db;

        public static List<Element> GetElementEntities()
        {
            try
            {
                if (db == null)
                {
                    db = new ElementEntities();
                }
                var Elements = from e in db.Elements
                    where e.RETRIEVE
                    orderby e.SORT_ORDER
                    select e;
                return Elements.ToList();
            }
            catch (MetadataException mdex)
            {
                //Exception is caught here every single time
                return null;
            }
        }

        //...
    }
}

The connection string for this assembly is exactly as EntityFramework made it:

<connectionStrings>
    <add name="ElementEntities" connectionString="metadata=res://*/ElementEntities.csdl|res://*/ElementEntities.ssdl|res://*/ElementEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=******;initial catalog=******;user id=******;password=******;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

I've verified that the data source, initial catalog, user id and password are all correct. Could there be a problem in some of the other data in the connection string? I've tried exchanging the * for the assembly name as follows:

<connectionStrings>
    <add name="ElementEntities" connectionString="metadata=res://ElementEntitiy/ElementEntities.csdl|res://ElementEntitiy/ElementEntities.ssdl|res://ElementEntitiy/ElementEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=******;initial catalog=******;user id=******;password=******;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

This method is called from a different assembly. Does that matter? I've heard that accessing EF objects from a different assembly can result in MetadataExceptions, but I assumed that what matters is the reference to ElementEntities, and that this wrapper method should resolve that.

One thing I have noticed is that "About Microsoft Visual Studio" lists .NET Framework 4.5.50938 while the EntityFramework Reference in the assembly lists a Runtime Version of v4.0.30319. Could this be responsible? If so, how would I update the EntityFramework?

Thanks to anyone who can help.

Devsman
  • 434
  • 4
  • 17
  • Check this [post](http://stackoverflow.com/questions/689355/metadataexception-unable-to-load-the-specified-metadata-resource) . Did you change any assembly name? – Axel Prieto Feb 18 '16 at 17:20
  • I've seen that question and tried each of those suggestions, with the exception of updating the EntityFramework to one with the same Runtime Version as .NET (because I don't know how to). No assembly names have changed. – Devsman Feb 18 '16 at 17:46

0 Answers0