[英]Many-to-many relationship between the same class
I'm trying to implement what seems like a pretty common case - a Friendship relationship between 2 users. 我正在尝试实现一个似乎很常见的情况-2个用户之间的友谊关系。 I think the models are self-explanatory. 我认为这些模型是不言自明的。 Friendship
needs to have 2 users on it and also some data about the relationship, and users have a Friendships
property, which is populated by any friendship they are a part of, as either User1
or User2
. Friendship
需要两个用户以及一些有关该关系的数据,并且用户具有一个Friendships
属性,该属性由他们所属的任何友谊(如User1
或User2
)填充。 I couldn't figure out a nicer way to name the 2 users. 我想不出一种更好的方式来命名2个用户。 Here are my models: 这是我的模型:
public class Friendship : Entity
{
public ApplicationUser User1 { get; set; }
public ApplicationUser User2 { get; set; }
...
}
public class ApplicationUser : IdentityUser
{
public virtual List<Friendship> Friendships { get; set; }
...
}
Here's my attempt at configuring the relationship in OnModelCreating
: 这是我在OnModelCreating
中配置关系的OnModelCreating
:
modelBuilder.Entity<ApplicationUser>()
.HasMany(x => x.Friendships)
.WithRequired()
.Map(t => t.MapKey("User1_Id", "User2_Id"));
I don't think I'm doing the configuration right. 我认为我的配置不正确。 Here's the error I'm getting when trying to create a migration from this: 这是尝试从中创建迁移时遇到的错误:
The specified association foreign key columns 'User1_Id, User2_Id' are invalid. The number of columns specified must match the number of primary key columns.
Is it possible to accomplish this using ef6? 是否可以使用ef6完成此操作? A special thanks to anyone that can help. 特别感谢任何可以提供帮助的人。
You are running into a multiplicity constraint . 您遇到了多重约束 。 The Friendship
class has two users which creates a cycle from ApplicationUser
-> Friendship
-> ApplicationUser
. Friendship
类有两个用户,它们从ApplicationUser
> Friendship
> ApplicationUser
创建一个循环。 To fix this remove the User1
and User2
property and add a collection ICollection<ApplicationUser> Users
. 要解决此问题,请除去User1
和User2
属性,并添加一个集合ICollection<ApplicationUser> Users
。
DTO: DTO:
public class ApplicationContext : DbContext
{
public ApplicationContext()
: base("ApplicationContext")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Relationship> Relationships { get; set; }
}
public class Entity
{
public int Id { get; set; }
}
public class User : Entity
{
public string Name { get; set; }
public virtual ICollection<Relationship> Relationships { get; set; }
}
public class Relationship : Entity
{
public virtual ICollection<User> Users { get; set; }
}
Sample: 样品:
var bob = new User
{
Name = "Bob",
Relationships = new List<Relationship>()
};
var fred = new User
{
Name = "Fred",
Relationships = new List<Relationship>()
};
var relationship = new Relationship
{
Users = new List<User>
{
bob,
fred
}
};
bob.Relationships.Add(relationship);
fred.Relationships.Add(relationship);
using(var context = new ApplicationContext())
{
context.Users.Add(bob);
context.Users.Add(fred);
context.Relationships.Add(relationship);
context.SaveChanges();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.