如何从 WCF 数据服务返回复杂类型?

发布于 2024-11-01 01:10:31 字数 1026 浏览 10 评论 0原文

是否可以从由实体类型集合组成的 WCF 数据服务返回复杂类型?

例如:

 //the complex type to return
 class Entities
 {
    ICollection<Contract> Contracts;
    ...

 }

 //configuration
 public partial class MyContext: DbContext
 {
    public MyContext()
        : base("name=DBEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.ComplexType<Entities>();
        modelBuilder.Entity<Contract>().HasKey(c=>c.Id);
        ...
    }
    ...
  }

 //the WCF Data Service
 public class PricingDataService : DataService<ObjectContext>, IDisposable
 {
     [WebGet]
     public Entities GetEntities()
     {
         return new Entities();
     }
  }

当我尝试上述配置时,出现异常:

" The exception message is 'One or more validation errors 
were detected during model     generation: System.Data.Edm.EdmEntityType: Name: 
Each type name in a schema must be unique. Type name 'Contract' was already 
defined."

Is it possible to return a complex type from a WCF data service that is made of collections of entity types ?

for example :

 //the complex type to return
 class Entities
 {
    ICollection<Contract> Contracts;
    ...

 }

 //configuration
 public partial class MyContext: DbContext
 {
    public MyContext()
        : base("name=DBEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.ComplexType<Entities>();
        modelBuilder.Entity<Contract>().HasKey(c=>c.Id);
        ...
    }
    ...
  }

 //the WCF Data Service
 public class PricingDataService : DataService<ObjectContext>, IDisposable
 {
     [WebGet]
     public Entities GetEntities()
     {
         return new Entities();
     }
  }

When I try the above configuration, I get an exception :

" The exception message is 'One or more validation errors 
were detected during model     generation: System.Data.Edm.EdmEntityType: Name: 
Each type name in a schema must be unique. Type name 'Contract' was already 
defined."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

我一向站在原地 2024-11-08 01:10:31

不,这是不可能的,因为如果您定义了它,则意味着复杂类型具有契约的导航属性。这在整个实体框架中是不允许的。该错误可能来自于一些推断,其中 Contract 已定义为实体,但复杂类型试图将其映射为其他内容 - 但这只是猜测。

No this is not possible because if you define this, it means that complex type has navigation property to contracts. This is not allowed in whole entity framework. The error probably comes from some infering where Contract is already defined as entity but complext type is trying to map it as something else - but this is just guess.

も星光 2024-11-08 01:10:31

由于某种原因,您发布的代码片段对我来说很难阅读。不过,我可以告诉你,我已经通过类似的 WCF 类型发送了。我通常不使用 ICollection<>为了声明我的列表,我倾向于使用 List<>相反,在接收客户端信息时从未遇到过太大问题。

也许这是您如何在数据契约上定义类的问题。

For some reason the snippet you posted is hard to read for me. However, I can tell you that I have sent via WCF types like those. I usually don't use ICollection<> to declare my lists, I tend to use List<> instead and have never had much of a problem receiving the information on the client.

Maybe it's a problem on how you're defining the class on your data contract.

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