将DbContext移动到具有不同项目的单独类中会导致异常

时间:2012-10-06 16:30:05

标签: c# entity-framework dbcontext

我正在为我的DAL使用Entity Framework(使用DBContext)。我们在关注分离方面遵循的分层体系结构,为此目的,生成的实体类必须进入单独的类项目。由于程序必须是可单元测试的,我使用Repository模式,因此我需要抽象我们从EF获得的DbContext。所有这些都意味着实体类和上下文类不在我的edmx文件所在的同一个项目中。问题是当我移动文件时,我开始得到以下异常

  

未处理的异常:System.Data.MetadataException:无法加载   指定的元数据资源在
  System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResou   rces(String assemblyName,String resourceName,ICollection 1 uriRegistry, Metada taArtifactAssemblyResolver resolver) at
System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource..ctor(Str ing originalPath, String assemblyName, String resourceName, ICollection
1 uriReg istry,MetadataArtifactAssemblyResolver resolver)   在
  System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateRes   sourceLoader(String path,ExtensionCheck extensionCheck,String   validExtension,I Collection 1 uriRegistry, MetadataArtifactAssemblyResolver resolver) at
System.Data.Metadata.Edm.MetadataArtifactLoader.Create(String path, Extens ionCheck extensionCheck, String validExtension, ICollection
1   uriRegistry,Metad ataArtifactAssemblyResolver resolver)在   在。的System.Data.Metadata.Edm.MetadataCache.SplitPaths(字符串路径)   System.Data.Common.Utils.Memoizer 2.<>c__DisplayClass2.<Evaluate>b__0() at System.Data.Common.Utils.Memoizer 2.Result.GetValue()at

  System.Data.Common.Utils.Memoizer 2.Evaluate(TArg arg) at
System.Data.EntityClient.EntityConnection.GetMetadataWorkspace(Boolean ini tializeAllCollections) at
System.Data.Objects.ObjectContext.RetrieveMetadataWorkspaceFromConnection( ) at System.Data.Objects.ObjectContext..ctor(EntityConnection connection, Boole an isConnectionConstructor) at
System.Data.Objects.ObjectContext..ctor(EntityConnection connection)
at
System.Data.Entity.Internal.InternalConnection.CreateObjectContextFromConn ectionModel() at
System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFrom ConnectionModel() at
System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType (Type entityType) at
System.Data.Entity.Internal.Linq.InternalSet
1.Initialize()在
  System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext()
at
System.Data.Entity.Infrastructure.DbQuery
1.System.Linq.IQueryable.get_Pro   System.Linq.Queryable.Where [TSource](IQueryable 1 source, Expression 1 pre dicate)的vider()   BusinessLayer.CustomerLocator.LocateCustomerByIdint id)in   C:\ Sandbox \ BusinessLayer \ CustomerLocator.cs:第41行   C:\ Sandbox \ Program.cs:line中的ClientApp.Program.Main(String [] args)   34

谷歌搜索表明在csdl,ssdsl等路径上可能出现问题,但我不确定这里有什么问题。以下是我的连接字符串设置方式

<add name="DBConnection" 
   connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=Customers;persist security info=True;user id=user;password=password;multipleactiveresultsets=True;App=EntityFramework&quot;" 
providerName="System.Data.EntityClient" />

0 个答案:

没有答案