C# 如何使用FLUENT API配置此模型关系
用户(AskedUser)可以有其他用户(Asker)提出的许多问题。 用户(询问者)可以向其他用户(询问者)提问。 因此,QuestionModel应该为被询问的用户id提供外键,并为提出问题的用户提供外键 我是否按照我想要实现的目标构建了我的模型?如何使用fluent api进行配置,因为仅使用数据注释无法实现这一点C# 如何使用FLUENT API配置此模型关系,c#,asp.net,entity-framework-core,ef-fluent-api,C#,Asp.net,Entity Framework Core,Ef Fluent Api,用户(AskedUser)可以有其他用户(Asker)提出的许多问题。 用户(询问者)可以向其他用户(询问者)提问。 因此,QuestionModel应该为被询问的用户id提供外键,并为提出问题的用户提供外键 我是否按照我想要实现的目标构建了我的模型?如何使用fluent api进行配置,因为仅使用数据注释无法实现这一点 public class ApplicationUser : IdentityUser<long> { public string FirstName {
public class ApplicationUser : IdentityUser<long>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string Country { get; set; }
public DateTime BirthDate { get; set; }
public ICollection<QuestionModel> AskedQuestions { get; set; }
}
public class QuestionModel
{
public int Id { get; set; }
public string Content { get; set; }
public bool IsAnswered { get; set; }
public long AskerId { get; set; }
public ApplicationUser Asker { get; set; }
public long AskedUserId { get; set; }
public ApplicationUser AskedUser { get; set; }
}
公共类应用程序用户:IdentityUser
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串City{get;set;}
公共字符串国家{get;set;}
公共日期时间出生日期{get;set;}
公共ICollection询问问题{get;set;}
}
公共类问题模型
{
公共int Id{get;set;}
公共字符串内容{get;set;}
公共布尔值被拒绝{get;set;}
公共长AskerId{get;set;}
公共应用程序用户询问器{get;set;}
公共长AskedUserId{get;set;}
公共应用程序用户AskedUser{get;set;}
}
这是我迄今为止一直在尝试的
builder.Entity<ApplicationUser>()
.HasMany(user => user.AskedQuestions)
.WithOne(q => q.AskedUser)
.HasForeignKey(user => user.AskedUserId)
.HasConstraintName("ForeignKey_User_AskedQuestion")
.HasForeignKey(user => user.AskerId)
.HasConstraintName("ForeignKey_Asker_QuestionAsked")
.IsRequired(true);
builder.Entity()
.HasMany(user=>user.AskedQuestions)
.WithOne(q=>q.AskedUser)
.HasForeignKey(user=>user.AskedUserId)
.hassConstraintName(“外键用户询问问题”)
.HasForeignKey(用户=>user.AskerId)
.HassConstraintName(“外国钥匙询问者提问”)
.IsRequired(正确);
您可以在QuestionModel
//询问者关系
builder.Entity()
.HasOne(q=>q.Asker)
.有很多(u=>u.提问)
.HasForeignKey(q=>q.AskerId)
.HassConstraintName(“外国钥匙询问者提问”)
.IsRequired(正确);
//询问关系
builder.Entity()
.HasOne(q=>q.AskedUser)
.有很多
.HasForeignKey(q=>q.AskeduserId)
.hassConstraintName(“外键用户询问问题”)
.IsRequired(正确);
我在依赖模型上使用Fluent API,而不是根元素。您的代码是否不起作用?有错误吗?