实体框架MetadataException

时间:2016-02-18 16:14:09

标签: c# .net entity-framework entity-framework-6

我已经看到很多类似问题的解决方案,但似乎没有解决我的问题。当我尝试在实体上执行Linq查询时,我抛出了MetadataException。

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;
            }
        }

        //...
    }
}

此程序集的连接字符串与EntityFramework完全相同:

<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>

我已经验证数据源,初始目录,用户ID和密码都是正确的。连接字符串中的某些其他数据是否存在问题?我已经尝试将*替换为程序集名称,如下所示:

<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>

从不同的程序集调用此方法。那有关系吗?我听说从不同的程序集访问EF对象可能会导致MetadataExceptions,但我认为重要的是对ElementEntities的引用,并且这个包装器方法应该解决这个问题。

我注意到的一件事是“关于Microsoft Visual Studio”列出了.NET Framework 4.5.50938,而程序集中的EntityFramework Reference列出了运行时版本v4.0.30319。这可能是负责任的吗?如果是这样,我将如何更新EntityFramework?

感谢任何可以提供帮助的人。

0 个答案:

没有答案
相关问题