【发布时间】:2011-09-23 23:50:54
【问题描述】:
我正在使用 EF4 和 WCF 数据服务。我想使用我当前的 SQL 连接并使用这种方法重新连接它,以便我可以从各种客户端(移动、Web、Win 客户端)访问数据。我知道我可以使用 LINQ,但我只是想知道使用存储过程来代替。
这是我的示例存储过程:
CREATE procedure getuserbyID (
@UserID int)
AS
SELECT Firstname, Lastname, Email From TestDatabase WHERE UserID = @UserID
在我的TestDataService.svc 中,我在InitialService 中有以下内容:
config.SetEntitySetAccessRule("Users", EntitySetRights.All);
config.SetServiceOperationAccessRule("GetUserByID", ServiceOperationRights.All
我还调用此处列出的文件中的存储过程:
[WebGet]
public List<User> GetUserByID(string UserID)
{
TestEntities entity = new TestEntities();
return entity.GetUserByID(Convert.ToInt32(UserID)).ToList();
}
在我的 EF 模型中,我已将存储的 proc 作为函数导入并将其保存为 GetUserByID,它返回一个 User 实体。
当我测试它时,我可以通过在浏览器中查看来访问实体:
http://localhost:1753/TestWcfDataService.svc 或 http://localhost:1753/TestWcfDataService.svc/Users 或 http://localhost:1753/TestWcfDataService.svc/Users(1) -- 示例用户 UserID 为 1
我知道它正在通过服务使用 LINQ 和 ODATA,但是当我尝试通过以下方式访问存储过程时:
http://localhost:1753/TestWcfDataService.svc/GetUserByID?UserID='1' -- 然后我收到一个无法显示页面的 HTTP 500 错误。我调试它并查看错误,这里是:
System.Data.EntityCommandExecutionException 未被用户代码处理 Message=The 数据阅读器不兼容 指定“TestModel.User”。成员 'UserID' 类型的没有 数据中的对应列 同名阅读器。
UserID 是我在Users 表中的主键和身份。任何帮助将不胜感激,并提前感谢您。
【问题讨论】:
-
只是猜测,但您的存储过程返回的列看起来像用户实体,但它们不包含关键属性 UserID。我认为 EF 需要关键属性值才能实现实体。您是否尝试将 UserID 作为存储过程中的列之一返回?
标签: stored-procedures wcf-data-services