我有一个类库项目,使用Entity Framework,它工作正常,直到我将模型移动到一个单独的类库类型项目.
当我将模型移出到一个名为Model的单独的类库项目后,我将app / web.config中的连接字符串更改为如下所示:
<add name="GlobalizationEntities" connectionString="Metadata=res://Model.dll/Models.ResourceGlobalizationModel.csdl| res://Model.dll/Models.ResourceGlobalizationModel.ssdl| res://Model.dll/Models.ResourceGlobalizationModel.msl; provider=System.Data.sqlClient; provider connection string=" Data Source=MyComputer\sqlexpress; Initial Catalog=DaDatabase; Integrated Security=true; MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
(我在上面的代码片段中包含换行符,只是为了可读性,我的代码没有那些换行符.)
现在,我的解决方案结构如下所示:
但是,在运行时我会收到一个FileNotFound异常:
System.IO.FileNotFoundException was unhandled by user code HResult=-2147024894 Message=Unable to resolve assembly 'Model.dll'. Source=System.Data.Entity StackTrace: at System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.ResolveAssemblyName(String assemblyName,MetadataArtifactAssemblyResolver resolver) at System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path,ExtensionCheck extensionCheck,String validExtension,ICollection`1 uriRegistry,MetadataArtifactAssemblyResolver resolver) at System.Data.Metadata.Edm.MetadataArtifactLoader.Create(String path,MetadataArtifactAssemblyResolver resolver) at System.Data.Metadata.Edm.MetadataCache.SplitPaths(String paths) at 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 initializeAllCollections) at System.Data.Objects.ObjectContext.RetrieveMetadataWorkspaceFromConnection() at System.Data.Objects.ObjectContext..ctor(EntityConnection connection,Boolean isConnectionConstructor) at System.Data.Objects.ObjectContext..ctor(String connectionString,String defaultContainerName) at Resources.BaseServices.Globalization.Models.ResourceGlobalizationEntities..ctor() in C:\SVN\Model\ResourceGlobalizationEntities.cs:line 7 at Resources.BaseServices.Globalization.Models.Culture.IsValidCulture(String shortName) in C:\SVN\Model\Culture.cs:line 24 at Resources.BaseServices.Globalization.EntityFrameworkStringResourceLoader.set_CultureName(String value) in C:\SVN\BusinessObjects\EntityFrameworkStringResourceLoader.cs:line 129 at Resources.BaseServices.Globalization.EntityFrameworkStringResourceLoader..ctor(String cultureName,IDataSource dataSource) in C:\SVN\BusinessObjects\EntityFrameworkStringResourceLoader.cs:line 19 InnerException:
我尝试了两个客户端的场景,即控制台应用程序和MVC应用程序.
虽然控制台应用程序甚至不将BusinessObjects.dll及其引用的dll(即Model.dll)复制到控制台应用程序的bin目录,但MVC应用程序也不会.但是,他们似乎根本找不到Model.dll程序集,并引发上述FileNotFoundException.
1)在连接字符串的所有位置,将res://Model.dll/更改为res:// Model /(不包含’.dll’扩展名).您还可以指定装配的全名,例如:res:// Model,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null /(更多信息:
https://msdn.microsoft.com/en-us/library/cc716756.aspx).
2)接下来,确保以下命名空间有效:Model.Models.ResourceGlobalizationModel.如果没有,请更改连接字符串和/或组合名称.