使用 EF 的数据服务 - 是否可以使用返回自定义类型的 WebGet 方法?
在我的项目中,我使用使用 EF 的数据服务。现在我有一个自定义类,我也想通过数据服务公开它,但我无法使其工作,似乎不可能在单个数据服务中混合自定义类型和 EF。有什么建议吗?
看起来它在元数据中找不到一些信息。
错误:
服务器遇到错误 处理请求。例外情况 消息是“无法加载元数据” 对于返回类型 'System.Linq.IQueryable<代码>1[ITS.NetProject.Model.CustomEnty]' 方法的 'System.Linq.IQueryable1[ITS.NetProject.Model.CustomEnty] GetCustomEnties()'.'。查看服务器日志 了解更多详情。异常堆栈 跟踪是:....
代码:
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class ITSServiceOData : DataService<ITSEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("GetCustomEnties",
ServiceOperationRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
[WebGet]
public IQueryable<CustomEnty> GetCustomEnties()
{
return from e in this.CurrentDataSource.CustomEnties select e;
}
}
///Here it is my model definition
namespace ITS.NetProject.Model
{
partial class ITSEntities
{
public IQueryable<CustomEnty> CustomEnties
{
get
{
return ...
}
}
}
//Company, Equipment, Owner are EF entity classes
[DataServiceKey("Id")]
public class CustomEnty
{
public int Id {get;set;}
public Subject Company { get; set; }
public Subject Equipment { get; set; }
public Subject Owner { get; set; }
}
}
In my project I use Data Service which use EF. Now I have a custom class which I also want to expose through my Data Service, but I can't make it work, it's seems like it is impossible to mix custom types and EF in single Data Service. Any suggestions?
Looks like it doesn't find some info in meta-data.
Error:
The server encountered an error
processing the request. The exception
message is 'Unable to load metadata
for return type
'System.Linq.IQueryable1[ITS.NetProject.Model.CustomEnty]'
1[ITS.NetProject.Model.CustomEnty]
of method
'System.Linq.IQueryable
GetCustomEnties()'.'. See server logs
for more details. The exception stack
trace is:....
Code:
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class ITSServiceOData : DataService<ITSEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("GetCustomEnties",
ServiceOperationRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
[WebGet]
public IQueryable<CustomEnty> GetCustomEnties()
{
return from e in this.CurrentDataSource.CustomEnties select e;
}
}
///Here it is my model definition
namespace ITS.NetProject.Model
{
partial class ITSEntities
{
public IQueryable<CustomEnty> CustomEnties
{
get
{
return ...
}
}
}
//Company, Equipment, Owner are EF entity classes
[DataServiceKey("Id")]
public class CustomEnty
{
public int Id {get;set;}
public Subject Company { get; set; }
public Subject Equipment { get; set; }
public Subject Owner { get; set; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,数据服务不支持该场景。您可以将自定义类型添加到 Entity Fx 模型中,但这远非理想......
No, Data Services do not support that scenario. You can add your custom type to your Entity Fx model, but that is far from ideal...
嗯 搭便车
- 将类和对象放入模型中并正确执行。根据上面的建议。
- 或制作单独的简单模型。使用相同的连接字符串。
进行令人讨厌的黑客攻击
或者当没有人在看啤酒 oClock 时 。 :-)
Hmmm free rider
- Put the class and Object in the model and do it properly. As per advice above.
- or Make separate simple model. Use same connection string.
or for that nasty hack...
when no one is looking at beer oClock. :-)