我正在尝试在约会表和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对象正在创建一个Join表。按预期称为BusinessServicEapointments。但是,它不会为ASPNETUSER(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核心的强制性。但是,我猜所有小小的帮助..
我已经看过这篇文章:在此网站上,但建议创建另一个我还没有看到需要做的表。它说的是"坏事发生"吗?因此,我想知道这实际上是通过身份/实体框架以某种方式阻止的,还是我只是错过了什么?
澄清:我知道我需要一个加入表。添加迁移代码并未创建它,而IT为BusinessServices-Appointments Relations。
我相信您需要一个联接表。
根据Microsoft的说法:"没有实体类代表联接表的许多人关系不支持。"
https://learn.microsoft.com/en-us/ef/core/modeling/relationships#other-relationshiphiphip-patterns