【发布时间】:2019-03-01 10:09:38
【问题描述】:
我正在尝试执行 3 个过程以构建客户对象并将其返回给客户端。但是,在我的 api(服务器端)中,我可以正确看到结果。但是,不幸的是,结果集中(客户端)中缺少 ICollection 数据
我正在使用 Entity Framework 和 OData
所以,这是我的模型:
public class Customer
{
[Key]
public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string VatCode { get; set; }
public string ChamberOfCommerceCode { get; set; }
public DateTime Modified { get; set; }
public DateTime Created { get; set; }
public string LanguageCode { get; set; }
public decimal Discount { get; set; }
public string CustomerManager { get; set; }
public Guid PriceList { get; set; }
public Guid PaymentCondition { get; set; }
// public bool VatLiable { get; set; }
public bool IsBlocked { get; set; }
public bool IsProspect { get; set; }
public bool IsSuspect { get; set; }
public string Website { get; set; }
public string DashboardUrl { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public ICollection<FreeFields> FreeFields { get; set; }
// [NotMapped]
// public Dictionary<string, string> UknownElements { get; set; }
[ForeignKey(nameof(Contacts.Customer))]
public ICollection<Contacts> Contact { get; set; }
[ForeignKey(nameof(Addresses.Customer))]
public ICollection<Addresses> Address { get; set; }
}
如您所见,我们在其中有 2 个 ICollections:联系人和地址。 这里是这两个的模型:
联系人型号
public class Contacts
{
[Key]
public Guid Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Initials { get; set; }
public string Function { get; set; }
[Column("Customer")]
public Guid Customer { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string LanguageCode { get; set; }
public bool IsMainContact { get; set; }
public string Gender { get; set; }
public string Username { get; set; }
}
和地址
public class Addresses
{
[Key]
public Guid Id { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string CountryCode { get; set; }
public string Type { get; set; }
[Column("Customer")]
public Guid Customer { get; set; }// This Property should be GUID instead of String..
public bool IsMainAddress { get; set; }
public string Route { get; set; }
public string State { get; set; }
}
现在,在我的控制器中,我从存储过程中获取数据并创建模型:
public IList<Customer> GetAllCustomers()
{
//Initialize the objects
IList<Customer> customers = null;
ICollection<Contacts> contacts = null;
ICollection<Addresses> addresses = null;
Dictionary<string, string> thisdict = null;
//Fetch the data from stored procedures
customers = db.Customers.FromSql("SIP_API_MONDIA_Customers_sel").ToList();
contacts = db.Contacts.FromSql("SIP_API_MONDIA_Contacts_sel").ToList();
addresses = db.Addresses.FromSql("SIP_API_MONDIA_Address_sel").ToList();
//Loop through customers and add the contact and addresses when required
foreach(var item in customers)
{
item.Contact = contacts.Where(x => x.Customer == item.Id).ToList();
item.Address = addresses.Where(x => x.Customer == item.Id).ToList();
// item.UknownElements = thisdict;
}
return customers;
}
Tihs 工作得很好,我可以通过断点看到 Customers 对象已被填充。地址和联系方式也正确。但是,一旦我将它返回给客户端,结果就会丢失两个 ICollections..
我觉得我错过了一个小细节,但我找不到它。
【问题讨论】:
-
XML 序列化在将接口集合作为成员名称时存在问题。也许这同样适用于 json。尝试换成更具体的集合接口或者具体的类(如
IList<>和List<>) -
问题是我无法访问客户端(它是第 3 方)并且在文档中他们明确要求 ICollection 编辑:转换为 ilist 以进行测试并没有解决它:(跨度>
标签: c# json rest asp.net-core