使用 EF 的数据服务 - 是否可以使用返回自定义类型的 WebGet 方法?

发布于 2024-10-22 03:53:07 字数 1783 浏览 4 评论 0原文

在我的项目中,我使用使用 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]'
of method
'System.Linq.IQueryable
1[ITS.NetProject.Model.CustomEnty]
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱贱哒 2024-10-29 03:53:07

不,数据服务不支持该场景。您可以将自定义类型添加到 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...

又怨 2024-10-29 03:53:07

嗯 搭便车
- 将类和对象放入模型中并正确执行。根据上面的建议。
- 或制作单独的简单模型。使用相同的连接字符串。

进行令人讨厌的黑客攻击

_context.Database.ExecuteSqlCommand(cmdText, parameters);   

或者当没有人在看啤酒 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...

_context.Database.ExecuteSqlCommand(cmdText, parameters);   

when no one is looking at beer oClock. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文