[英]C# Entity Framework In Memory Db Relationships
我有一个简单的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; }
}
您的关系定义不正确。如何在ClientModel
类中实现这种关系。
public List<UserModel> Users { get; set; }
public UserModel Advisor { get; set; }
实体框架会发现这种模棱两可的关系。 应该是一对一或一对多。 可能是您在这里混在一起。 只需删除first或last(根据您的要求)并尝试运行它即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.