我有一个简单的API,其中包含三个级别的数据:
帐户>用户>地址
API已成功加载用户的地址,但引发了确定客户端与用户之间关系的异常。
Unhandled Exception: System.InvalidOperationException: Unable to determine the relationship represented by navigation property 'ClientModel.Users' of type 'List<UserModel>'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
以下是与我相同的模型:
public class ClientModel
{
public string Id { get; set; }
public string Type { get; set; }
public string DisplayName { get; set; }
public string Email { get; set; }
public long Phone { get; set; }
public string Photo { get; set; }
public string Source { get; set; }
public string CreatedAt { get; set; }
public int AccountsCount { get; set; }
public double AccountsBalance { get; set; }
public double AccountsBalancePending { get; set; }
public double AccountsDonations { get; set; }
public double AccountsBalanceAch { get; set; }
public double AccountsBalanceIra { get; set; }
public List<UserModel> Users { get; set; }
public UserModel Advisor { get; set; }
}
public class UserModel
{
public string Id { get; set; }
public string ClientId { get; set; }
public ClientModel Client { get; set; }
public string Role { get; set; }
public bool Primary { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Token { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Photo { get; set; }
public string Birthday { get; set; }
public long Phone { get; set; }
public List<AddressModel> Address { get; set; }
}
public class AddressModel
{
public string Id { get; set; }
public string UserId { get; set; }
public UserModel User { get; set; }
public string Street { get; set; }
public string StreetSecondary { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
}
答案 0 :(得分:1)
您的关系定义不正确。在您的关系中怎么可能
ClientModel
类。
public List<UserModel> Users { get; set; }
public UserModel Advisor { get; set; }
实体框架会发现这种模棱两可的关系。应该是一对一或一对多。可能是您在这里混在一起。只需删除first或last(根据您的要求)并尝试运行它即可。