如何从 WCF 数据服务返回复杂类型?
是否可以从由实体类型集合组成的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这是不可能的,因为如果您定义了它,则意味着复杂类型具有契约的导航属性。这在整个实体框架中是不允许的。该错误可能来自于一些推断,其中
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.由于某种原因,您发布的代码片段对我来说很难阅读。不过,我可以告诉你,我已经通过类似的 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.