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="data source=******;initial catalog=******;user id=******;password=******;multipleactiveresultsets=True;application name=EntityFramework"" 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="data source=******;initial catalog=******;user id=******;password=******;multipleactiveresultsets=True;application name=EntityFramework"" 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.