我正在尝试从我的 wcf 服务返回一个自定义类。我的代码在下面并出现错误。
服务器在处理请求时遇到错误。异常消息是 'DealerModel' 类型的属性 'UserList' 不是有效属性。不支持类型为基元或复杂类型集合的属性。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是:
我正在尝试获取如下所示的 json 值;
<Dealer>
<DealerName/>
<DealerAdress/>
<Users>
<FirstName/>
<LastName/>
<FirstName/>
<LastName/>
</Users>
</Dealer>
我的代码
using System;
using System.Linq;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
public class DataModel:IDisposable
{
private xModel.xEntities _dbContext = new xSocialModel.xEntities();
public DataModel()
{
Users = from e in _dbContext.Users
select new UserModel
{
UserID = e.UserID,
Firstname = e.Firstname,
Lastname = e.Lastname
};
Dealers = from e in _dbContext.Dealers
select new DealerModel
{
DealerID = e.DealerID,
DealerName = e.Name,
DealerAddress = e.Address,
UserList = e.Users
};
}
/// <summary>Returns the list of users.</summary>
public IQueryable<UserModel> Users { get; private set; }
public IQueryable<DealerModel> Dealers { get; private set; }
void IDisposable.Dispose()
{
_dbContext.Dispose();
}
}
using System;
using System.Data.Services.Common;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.ServiceModel;
using System.Data.Objects.DataClasses;
using System.Linq;
/// <summary>Represents a User.</summary>
[DataServiceKey("UserID")]
public class UserModel
{
public Guid UserID { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
[DataServiceKey("DealerID")]
public class DealerModel
{
public Guid DealerID { get; set; }
public string DealerName { get; set; }
public string DealerAddress { get; set; }
**public EntityCollection<xModel.User> UserList { get; set; }**
}