我正在尝试使用DotNetSDataClient库在Infor中添加一个新的联系人。我尝试在Create部分下跟踪。当我运行示例代码时,我会收到错误“联系人的帐户是必需的”。这是有意义的,因为我相信数据库中的每个联系人都必须与一个帐户相关联。我修改了代码以指定一个现有帐户,但现在我收到了错误“我们很抱歉,您遇到了错误。如果适用,请再试一次。”对于内部异常,“remove服务器返回一个错误:(500)内部服务器错误”。
这是我的密码。
public void someFunction(){
var client = new SDataClient("https://domain/sdata/slx/dynamic/-/")
{
UserName = "username",
Password = "password"
};
var contact = new Contact
{
Account = new Account
{
AccountName = "accountName",
Id = "accountId"
},
Address = new Address
{
Address1 = "1234 Address",
City = "someCity",
PostalCode = "12345",
State = "ST"
},
FirstName = "John",
LastName = "Doe"
};
var contactOptions = new SDataPayloadOptions { Include = "Address" };
try
{
contact = client.Post(contact, null, contactOptions);
}
catch (Exception ex)
{
var error = ex.Message;
}
}
[SDataPath("accounts")]
public class Account
{
[SDataProtocolProperty(SDataProtocolProperty.Key)]
public string Id { get; set; }
public string AccountName { get; set; }
public List<Contact> Contacts { get; set; }
public string Status { get; set; }
public string Type { get; set; }
}
[SDataPath("contacts")]
public class Contact
{
[SDataProtocolProperty(SDataProtocolProperty.Key)]
public string Id { get; set; }
public Account Account { get; set; }
public Address Address { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string FullName { get; set; }
public string LastName { get; set; }
public DateTime? ModifyDate { get; set; }
public string Status { get; set; }
}
[SDataPath("addresses")]
public class Address
{
[SDataProtocolProperty]
public string Key { get; set; }
public string Address1 { get; set; }
public string Address3 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string CountryCode { get; set; }
public string Description { get; set; }
public string PostalCode { get; set; }
public string State { get; set; }
public string Street { get; set; }
}有人知道我做错了什么吗?
发布于 2016-12-14 13:42:53
我还在GitHub上发布了这个问题,Ryan给出了答案。我想把它包括在这里,以防其他人需要这个解决方案。
问题在于库如何序列化数据。以下是瑞安的回应:
“这里真正的问题是POCO有一个联系人集合。DotNetSDataClient将其序列化为null,而SData服务器不喜欢这样。即使将该集合设置为新List(),也会失败,因为它将被序列化为[]。
"Contacts": { "$resources": [] }当使用现有的父实体或相关实体进行发布时,SData希望只接收$key,而不接收其他任何内容。因此,当Contact类被序列化时,您应该用联系人数据发送的内容是
"Account": { "$key":"AXXX00000001" }仅此而已,但事实并非如此,这个库正在序列化和发送所有的东西。“
此时,解决方案是创建一个只具有id (key)属性的帐户类。
[SDataPath("accounts")]
public class Account
{
[SDataProtocolProperty(SDataProtocolProperty.Key)]
public string Id { get; set; }
}在某些时候,DotNetSDataClient库可能会被更新以处理这种情况,但现在这是解决方案。
https://stackoverflow.com/questions/41125172
复制相似问题