[英]Navigating in both directions in one2one relationship
I am using AspNetIdentity and with that the generated model for AspNetUser.我正在使用 AspNetIdentity 并为 AspNetUser 生成 model。 I have created my own AspNetUser class like so:
我已经创建了自己的 AspNetUser class,如下所示:
public class MieterverwaltungUser : IdentityUser<int>
{
public override int Id { get; set; }
[NotMapped]
public TenantData TenantData { get; set; }
}
The TenantData is not mapped because I don't want it to be shown in the table and only use it for navigation purposes in my code. TenantData 未映射,因为我不希望它显示在表中,并且仅在我的代码中将其用于导航目的。 TenantData however has a reference to it:
然而,TenantData 引用了它:
public class TenantData
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public string Nationality { get; set; }
public Gender Gender { get; set; }
public Job Job { get; set; }
public MieterverwaltungUser MieterverwaltungUser { get; set; }
}
Now I am trying to select a User and include the TenantData like so:现在我正在尝试 select 一个用户并像这样包含 TenantData:
var user = await _mieterverwaltungContext.Users.Include(x => x.TenantData).SingleOrDefaultAsync(x => x.NormalizedEmail == email.ToUpper());
But I am getting an error:但我收到一个错误:
System.InvalidOperationException: Lambda expression used inside Include is not valid.
System.InvalidOperationException:包含内使用的 Lambda 表达式无效。
Thanks in advance!提前致谢!
Fixed it by removing the [NotMapped]
Annotation and adding通过删除
[NotMapped]
注释并添加来修复它
public int MieterverwaltungUserId { get; set; }
in TenantData
, so that EFC knows where to put the foreign key.在
TenantData
中,以便 EFC 知道将外键放在哪里。 Now I can use the code above without problems.现在我可以毫无问题地使用上面的代码了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.