一个人可以有很多同事,而一个同事就是一个人。是否可以在ColleagueId
和上创建集群键PersonId
?
好的,我不确定,但是Colleague类仅在那儿,因为数据库应该了解与的连接Person
。所以,实际上我不需要Colleague
上课。我该怎么做,数据库知道该Person
列表Colleagues
有一个Person
?
在程序中,我们可以创建Persons
,然后应该可以将其他添加Persons
到!的Persons
列表中Colleagues
。
我的解释令人困惑,但我不知道如何以其他任何方式进行解释。
同事班:
public class Colleague
{
[Key]
public int ColleagueId { get; set; }
[Key]
public virtual Person PersonId { get; set; }
}
人类:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public byte[] Image { get; set; }
public virtual ICollection<Conversation> Conversations { get; set; }
public virtual ICollection<Colleague> Colleague { get; set; }
public Person()
{
Conversations = new List<Conversation>();
Colleague = new List<Colleague>();
}
}
您基本上需要Person
实体之间的多对多关系。为此,您将需要一个连接Person
实体对的联结表。如果我们称这个实体为WorkRelationship
:
class WorkRelationship
{
[Key]
[Column(Order = 1)]
[ForeignKey("Myself")]
public int MyselfId { get; set; }
public virtual Person Myself { get; set; }
[Key]
[Column(Order = 2)]
[ForeignKey("Colleague")]
public int ColleagueId { get; set; }
public virtual Person Colleague { get; set; }
}
然后我们Person
像这样修改:
class Person
{
public int Id { get; set; }
public string Name { get; set; }
[InverseProperty("Myself")]
public virtual ICollection<WorkRelationship> WorkRelationships { get; set; }
public void AddWorkRelationship(Person colleague)
{
if (WorkRelationships == null)
{
WorkRelationships = new List<WorkRelationship>();
}
WorkRelationships.Add(new WorkRelationship { Myself = this, Colleague = colleague });
}
}
因此,您可以看到Person
现在有了的集合WorkRelationships
:通过向该集合添加一个实体,您不仅可以将此人链接到他/她的同事,还可以创建反向关系。我还添加了一个辅助方法,以便您可以轻松添加关系。
这是一个非常基本的数据库上下文类,可用于管理这些实体:
sealed class MyContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<WorkRelationship> WorkRelationships { get; set; }
public IEnumerable<Person> GetColleagues(int personId)
{
List<WorkRelationship> relationships =
WorkRelationships
.Include(x => x.Myself)
.Include(x => x.Colleague)
.Where(x => x.MyselfId == personId || x.ColleagueId == personId)
.ToList();
foreach (WorkRelationship relationship in relationships)
{
if (relationship.Myself.Id == personId)
{
yield return relationship.Colleague;
}
else if (relationship.Colleague.Id == personId)
{
yield return relationship.Myself;
}
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}
}
我还为此添加了一个辅助方法,该方法将检索给定人员的同事。
现在,我们可以创建一个简单的测试程序来插入和查询人员:
static void Main(string[] args)
{
var alice = new Person { Name = "Alice" };
var bob = new Person { Name = "Bob" };
var colin = new Person { Name = "Colin" };
var davina = new Person { Name = "Davina" };
alice.AddWorkRelationship(bob);
alice.AddWorkRelationship(colin);
bob.AddWorkRelationship(davina);
using (var db = new MyContext())
{
db.People.AddRange(new[] { alice, bob, colin, davina });
db.SaveChanges();
}
using (var db = new MyContext())
{
Console.WriteLine("Bob works with:");
foreach (Person colleague in db.GetColleagues(bob.Id))
{
Console.WriteLine(colleague.Name);
}
}
Console.ReadLine();
}
以下是原始答案(包含在上下文中)
如果Colleague
is是-a Person
,那么您应该像这样建模:
public class Colleague : Person
{
// don't need any other properties based on what you've posted
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public byte[] Image { get; set; }
public virtual ICollection<Conversation> Conversations { get; set; }
public virtual ICollection<Colleague> Colleagues { get; set; }
}
(我将用于保存同事集合的属性名称进行了复数处理)。
然后,EF Code First应该创建一个Person
带有Discriminator
用于区分Person
和Colleague
实体的额外列的单个表。
再考虑一下,我什至不确定您是否需要单独的Colleague
实体。您可能会摆脱这种情况:
public class Person
{
...
public virtual ICollection<Person> Colleagues { get; set; }
}
请注意,ICollection<Person>
而不是ICollection<Colleague>
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句