我有两个实体:
public class Clientdetails
{
public int ClientId { get; set; }
public string CompanyName { get; set; }
public sbyte IndustryId { get; set; }
public int BusinessModelId { get; set; }
public Logincredentials Client { get; set; }
}
public partial class Logincredentials
{
public int UserId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public sbyte RoleId { get; set; }
public bool IsActive { get; set; }
public Rolemaster Role { get; set; }
public Clientdetails Clientdetails { get; set; }
}
我想更新ClientDetails实体,我想要更新Logincredentials实体。这是导航财产
我试过以下代码:
public Clientdetails UpdateClient(Clientdetails client)
{
var details = _unitOfWork.ClientDetailsRepository.FindBy(c => c.ClientId == client.ClientId).Include(c=>c.Client).SingleOrDefault();
_entities.Entry(details).State = EntityState.Modified;
_entities.SaveChanges();
}
我正在传递这样的数据:
{"clientId":1,"companyName":"Hewlett Packard1",
"industryId":2,"businessModelId":2,
"client":{"userId":1,"email":"priya.dave@xyz.com","isActive":true}
}
它更新了ClientDetails实体的字段,但它没有更新导航属性的属性,即LoginCredentials。
如果我手动分配属性,那么它可以工作:
var details = _unitOfWork.ClientDetailsRepository.FindBy(c => c.ClientId == client.ClientId).Include(c=>c.Client).SingleOrDefault();
if (details != null)
{
if (client.Client != null)
{
if (!string.IsNullOrEmpty(client.Client.Email))
{
details.Client.Email = client.Client.Email;
}
}
}
但我想摆脱手工作业。如果它在手动分配时有效,如果我只是将输入修改后的实体分配给详细信息对象,为什么它不起作用? 我在这里错过了什么?