我在一个单独的C#类库项目中构建了我的实体模型,而不是我的web项目。在我单独的类库项目中,我创建了一些简单的类,这些类是实体类的表示,用于通过AJAX传递给客户端。在简单对象中,我创建了用于查询数据库的上下文。当我从我的Web应用程序调用这些类中的方法时,我似乎无法得到错误。起初是因为我的web.config文件中没有任何内容引用项目中与实体模型的连接字符串。我将app.config文件中的三个标签复制到我的web.config文件中,但现在我看到一个错误:不支持关键字:尝试查询数据库时出现'元数据'。
我不确定的一件事是创建上下文的位置。应该在Web项目中完成,然后传递给类库项目中的方法?当我尝试这种方法时,我收到以下错误:
错误CS0012:类型'System.Data.Entity.DbContext'在未引用的程序集中定义。您必须添加对程序集'EntityFramework,Version = 5.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的引用。
如果我在类库项目中创建上下文,在运行应用程序并尝试查询数据库时,我得到的关键字不受支持:'metadata'错误。所以我不知道该去哪里。我究竟做错了什么?我花了几个小时寻找答案但是有太多的信息我找不到任何具体的信息。
提前致谢。
答案 0 :(得分:7)
使用EntityFramework创建类库时,VS会自动将其添加为已安装的包,但仅适用于类库项目。您必须返回NuGet包管理器以获取解决方案,并将该包添加到您的其他项目中
答案 1 :(得分:0)
我通过编辑web.config文件来解决这个问题。我需要将providerName从System.Data.SqlClient更改为System.Data.EntityClient。我不确定它是如何切换但现在它正在工作。还要注意上面的注释以摆脱你必须添加对程序集'EntityFramework,Version = 5.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的引用。“错误。
答案 2 :(得分:0)
在我的情况下,我遇到了类似的问题,但有以下错误:
Error 1 The type 'System.Data.Entity.DbContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Projects\MyProject\MyProject.Model.Net\ProductManager.cs 16 17 MyProject.Model.Net
Error 2 The type 'System.Data.Entity.DbSet`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Projects\MyProject\MyProject.Model.Net\ProductManager.cs 17 17 MyProject.Model.Net
Error 3 'System.Data.Entity.DbSet`1<MyProject.Data.ProductInventoryView>' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Data.Entity.DbSet`1<MyProject.Data.ProductInventoryView>' could be found (are you missing a using directive or an assembly reference?) C:\Projects\MyProject\MyProject.Model.Net\ProductManager.cs 18 21 MyProject.Model.Net
Error 4 Metadata file 'C:\Projects\MyProject\MyProject.Model.Net\bin\Debug\MyProject.Model.Net.dll' could not be found C:\Projects\MyProject\MyProject.Web\CSC MyProject.Web
要解决此问题,我需要通过浏览到此目录来添加EntityFramework.dll引用MyProject.Data项目以选择文件:〜\ MyProject \ packages \ EntityFramework.5.0.0 \ lib \ net40 \ EntityFramework.dll