如何在ASP.Net MVC 5中处理多个表(用户)-Fluent API [英] How to handle many to many same table (User) in ASP.Net MVC 5 - Fluent API
问题描述
我正在学习ASP.Net MVC 5,并且坚持使用一种基本的DB设计. 所以,我有一个用户,可以推荐很多人在工作 此外,许多人都可以申请获得推荐.我已经创建了两个角色,所有这些都已得到照顾.现在,我在名为 Referral 的课程上,它将跟踪需要完成的每个Referral实例.
I am learning ASP.Net MVC 5 and I am stuck with one basic DB Design. So, I have one User which can refer many person in job Also, many person can apply to get themselves Referred. I have created two roles and all of that is taken care. Now, I have on class called Referral which will keep track of every instance of Referral that needs to be done.
引荐模型:
Public class Referral
{
[Key]
public int ReferralId { get; set; }
public Company Company { get; set; }
public int CompanyId { get; set; }
public ApplicationUser User { get; set; }
public string CandidateId { get; set; } // Application User ID of person asking for referral
public string ReferrerId { get; set; } // Application user ID of person referring the candidate
}
ApplicationUser模型
ApplicationUser Model
public class ApplicationUser : IdentityUser
{
public ICollection<Referral> Referrals { get; set; }
// rest prop removed for brevity sake
}
现在,假设A(推荐人)引用B(候选人).我的表格行将如下所示.
Now, suppose A(Referrer) refers B(Candidate). My Table row will look like below.
ReferralId CompanyId CandidateId ReferrerId
1 1 B A
那么好.但是我想在推荐表上建立FK关系.我是Fluent API的新手,但我尝试如下进行操作.
So, far so good. But I want to establish FK relationship on Referral table. I am new to Fluent API but I tried like below.
// one candidate can have many referrals
dBModelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.Referrals)
.WithRequired(u => u.User)
.HasForeignKey(u => u.CandidateId)
.WillCascadeOnDelete(false);
//one referrar can have many referrals
dBModelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.Referrals)
.WithRequired(u => u.User)
.HasForeignKey(u => u.ReferrerId);
但是EF只尊重一种关系.为什么两个外键关系都没有设置.如果我注释掉一个,那么其他作品,反之亦然.但是,如图所示将它们放在一起永远不会起作用.
But EF respects only one relationship. Why both the foreign key relationhip is not getting set. If I comment out one then other works, and vice versa. But keeping them together as shown never works.
预期的行为:我希望有两个FK关系.一旦有了这些,我就可以相应地工作.请在这里指导我.我是这一切的新手.
Expected Behaviour: I expected to have two FK relationship. Once I have that then I can work accordingly. Please guide me here. I am new to all this.
推荐答案
正如评论中提到的@SLaks一样,要具有两个关系,您需要两个导航属性(每个FK一个).
As @SLaks mentioned in the comments, in order to have two relationships, you need two havigation properties (one for each FK).
因此,在 one 端,将Referrals
属性替换为以下内容:
So in the one side replace Referrals
property with something like this:
public class ApplicationUser : IdentityUser
{
// ...
public ICollection<Referral> ReferrerOf { get; set; }
public ICollection<Referral> CandidateOf { get; set; }
}
在许多端将User
属性替换为:
at many side replace the User
property with:
public class Referral
{
// ...
public ApplicationUser Candidate { get; set; }
public ApplicationUser Referrer { get; set; }
}
并将它们与流利的API相关联:
and correlate them with fluent API:
modelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.CandidateOf) // <--
.WithRequired(r => r.Candidate) // <--
.HasForeignKey(r => r.CandidateId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.ReferrerOf) // <--
.WithRequired(r => r.Referrer) // <--
.HasForeignKey(r => r.ReferrerId);
只要正确地关联了导航属性的名称,它们实际上就无关紧要.
The names of the navigation properties don't really matter as soon as you correlate them correctly.
这篇关于如何在ASP.Net MVC 5中处理多个表(用户)-Fluent API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!