public class Event
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public Guid GuidId { get; set; }
public virtual EventType Type { get; set; }
public string Title { get; set; }
public string Desc { get; set; }
public Guid Author { get; set; }
public bool IsAllowed { get; set; }
public DateTime DateStart { get; set; }
public DateTime DateEnd { get; set; }
public string Place { get; set; }
public Guid Responsible { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
public virtual ICollection<EventСomment> Comments { get; set; }
public virtual ICollection<EventWorker> Workers { get; set; }
public virtual ICollection<EventMember> Members { get; set; }
public virtual ICollection<EventInvitation> invitations { get; set; }
public class EventInvitation
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public Guid GuidId { get; set; }
public string Type { get; set; }
public int EventId { get; set; }
[JsonIgnore]
public virtual Event Event { get; set; }
public Guid Persone { get; set; }
public string Comment { get; set; }
public Guid Author { get; set; }
public bool IsApproved { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
}
public class EventMember
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public Guid GuidId { get; set; }
public int EventId { get; set; }
[JsonIgnore]
public virtual Event Event { get; set; }
public Guid Person { get; set; }
public string Comment { get; set; }
public Guid Author { get; set; }
public bool IsApproved { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
}
这些是我用于 PostgreSQL 数据库的 3 个实体框架模型。正如您所看到的,这是主要的事件模型,其中包含 EventInvitation 和 EventMember 的集合。
问题是我想在EventInvitation和EventMember之间建立1对1的关系,但是我的所有尝试都会导致与Event的一些关键冲突,我不知道如何正确地做到这一点。 也许我的想法从根本上来说是错误的,我只是试图做不可能的事。无论如何,这是我的上下文类来显示我已经拥有的连接:
public class DataContext: DbContext
{
public DbSet<Event> Events{ get; set; }
public DbSet<EventInvitation> Invitations{ get; set; }
public DbSet<EventMember> Members { get; set; }
public DbSet<EventType> Types { get; set; }
public DbSet<EventWorker> Worker { get; set; }
public DbSet<EventСomment> Comments { get; set; }
public DbSet<Relation> Relations { get; set; }
public DataContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(DataContext).Assembly);
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Event>()
.HasMany(p => p.Members)
.WithOne(c => c.Event)
.HasForeignKey(c => c.EventId);
modelBuilder.Entity<Event>()
.HasMany(p => p.invitations)
.WithOne(c => c.Event)
.HasForeignKey(c => c.EventId);
modelBuilder.Entity<Event>()
.HasMany(p => p.Comments)
.WithOne(c => c.Event)
.HasForeignKey(c => c.EventId);
}
要在
EventInvitation
和 EventMember
之间建立 1:1 关系,您需要决定主要实体和从属实体。依赖实体的主键也将充当主体实体的外键。
对于此示例,我们假设
EventInvitation
是主要实体,EventMember
是从属实体。这意味着对于每个EventInvitation
,最多可以有一个EventMember
,并且EventMember
的主键也将是EventInvitation
的外键。
设置方法如下:
public class EventInvitation
{
// ... other properties ...
public virtual EventMember Member { get; set; }
}
public class EventMember
{
[Key]
[ForeignKey("EventInvitation")] // This indicates that the primary key is also a FK to EventInvitation
public int Id { get; set; }
// ... other properties ...
public virtual EventInvitation Invitation { get; set; }
}
OnModelCreating
方法:protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// ... other configurations ...
modelBuilder.Entity<EventInvitation>()
.HasOne(i => i.Member)
.WithOne(m => m.Invitation)
.HasForeignKey<EventMember>(m => m.Id); // Indicates that EventMember's Id is the FK to EventInvitation
// ... other configurations ...
}
这样做:
EventInvitation
有一个 EventMember
,并且 EventMember
的主键也充当 EventInvitation
的外键。EventInvitation
导航属性从 EventMember
导航到其对应的 Member
,反之亦然,使用 Invitation
导航属性。请记住,对于 1:1 关系,您需要确保数据遵守此约束。创建
EventMember
时,其 Id
应与现有 Id
的 EventInvitation
匹配。
如果您决定
EventMember
应该是主要实体,您将反转配置。