相同class之间的多对多关系
Many-to-many relationship between the same class
我正在尝试实现一个看起来很常见的案例 - 两个用户之间的友谊关系。我认为这些模型是不言自明的。 Friendship
需要有 2 个用户以及一些关于关系的数据,用户有一个 Friendships
属性,其中包含他们所属的任何友谊,作为 User1
或 User2
。我想不出更好的方法来命名这 2 个用户。这是我的模型:
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; }
...
}
这是我在 OnModelCreating
中配置关系的尝试:
modelBuilder.Entity<ApplicationUser>()
.HasMany(x => x.Friendships)
.WithRequired()
.Map(t => t.MapKey("User1_Id", "User2_Id"));
我认为我的配置不正确。这是我尝试从中创建迁移时遇到的错误:
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.
是否可以使用 ef6 完成此操作?特别感谢任何可以提供帮助的人。
你运行变成了multiplicity constraint。 Friendship
class 有两个用户,从 ApplicationUser
-> Friendship
-> ApplicationUser
创建一个循环。要解决此问题,请删除 User1
和 User2
属性 并添加集合 ICollection<ApplicationUser> Users
.
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; }
}
样本:
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();
}
我正在尝试实现一个看起来很常见的案例 - 两个用户之间的友谊关系。我认为这些模型是不言自明的。 Friendship
需要有 2 个用户以及一些关于关系的数据,用户有一个 Friendships
属性,其中包含他们所属的任何友谊,作为 User1
或 User2
。我想不出更好的方法来命名这 2 个用户。这是我的模型:
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; }
...
}
这是我在 OnModelCreating
中配置关系的尝试:
modelBuilder.Entity<ApplicationUser>()
.HasMany(x => x.Friendships)
.WithRequired()
.Map(t => t.MapKey("User1_Id", "User2_Id"));
我认为我的配置不正确。这是我尝试从中创建迁移时遇到的错误:
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.
是否可以使用 ef6 完成此操作?特别感谢任何可以提供帮助的人。
你运行变成了multiplicity constraint。 Friendship
class 有两个用户,从 ApplicationUser
-> Friendship
-> ApplicationUser
创建一个循环。要解决此问题,请删除 User1
和 User2
属性 并添加集合 ICollection<ApplicationUser> Users
.
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; }
}
样本:
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();
}