我正在尝试在约会表和aspnetuser表(SGGUser)之间创建多对多关系。 我首先在EF 6中使用代码。 我的约会对象是这样:
public class Appointment
{
[Key]
[JsonProperty]
public int Id { get; set; }
[JsonProperty]
public SGGUser CreatorOwner { get; set; }
[JsonProperty]
public Address LocationAddress { get; set; }
[JsonProperty]
public string Notes { get; set; }
[JsonProperty] // Set to an ICollection to allow multiple people involved
public virtual ICollection<SGGUser> Participants { get; set; }
[JsonProperty] // Set to an ICollection to allow multiple services
public virtual ICollection<BusinessService> BusinessServices { get; set; }
}
SGGUser是这样的:
public class SGGUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<SGGUser> manager, string authenticationType = DefaultAuthenticationTypes.ApplicationCookie)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
...more stuff
public virtual ICollection<Appointment> Appointments { get; set; }
}
在约会表中,BusinessServices对象正在创建应有的联接表。按预期方式调用BusinessServiceAppointments。 但是,它不会为ASPNETUSERS(SGGUSER)表和约会创建连接表。 我尝试将其添加到上下文类(SGGContext):
public class SGGContext : IdentityDbContext<SGGUser>
{
public SGGContext()
: base("SGGContext", throwIfV1Schema: false)
{
}
... more stuff
public DbSet<BusinessService> BusinessServices { get; set; }
public DbSet<Appointment> Appointments { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<SGGUser>()
.HasMany(u => u.Appointments);
modelBuilder.Entity<Appointment>()
.HasMany(ap => ap.Participants);
}
}
尽管,我给人的印象是这仅对于EF核心是强制性的。但是,我猜想每个小小的帮助。
我看过这篇文章:On this site,但它建议创建另一个我还没有看到需要做的表。它说的一切都是“坏事发生了”?因此我不禁要问,实际上是Identity / Entity框架阻止了这种情况,还是我只是错过了什么?
说明:我知道我需要一个联接表。添加迁移代码不会创建它,而它是为BusinessServices-Appointments关系创建的。
答案 0 :(得分:-1)
我相信您将需要一个联接表。
根据Microsoft的说法,“尚不支持没有实体类来表示联接表的多对多关系。”
https://docs.microsoft.com/en-us/ef/core/modeling/relationships#other-relationship-patterns