为 entity framework 创建与其他记录有关系的记录:ASP.NET Core 6 Minimal API (SQL / Postgres)
Creating record for entity framework with relationships to other records : ASP.NET Core 6 Minimal API (SQL / Postgres)
我有两个 table,Participant
和 Meeting
,在一个 ASP.NET Core 6 minimal API 项目中设置如下:
record Participant(int id)
{
public string name { get; set; } = default!;
public string title { get; set; } = default!;
}
record Meeting(int id)
{
public string subject { get; set; } = default!;
public string organizer { get; set; } = default!;
public DateTime starttime { get; set; } = default!;
public DateTime endtime { get; set; } = default!;
}
class MyDb: DbContext
{
public MyDb(DbContextOptions<MyDb> options): base(options)
{
}
public DbSet<Participant> Participants => Set<Participant>();
public DbSet<Meeting> Meetings => Set<Meeting>();
}
现在我需要创建一个组合 table/记录来连接这两个 tables/记录
在 SQL 中就是这样 table
CREATE TABLE meetings_participants
(
meeting_id int NOT NULL,
participant_id int NOT NULL,
PRIMARY KEY (meeting_id, participant_id),
CONSTRAINT fk_meeting_id
FOREIGN KEY (meeting_id) REFERENCES meetings(id),
CONSTRAINT fk_participant_id
FOREIGN KEY (participant_id) REFERENCES participants(id)
);
但是我如何在 EF 中将它写为带有外键的记录?
您可以像这样创建一个 MeetingParticipant class:
public class MeetingParticipant
{
public int ParticipantID { get; set; }
[ForeignKey("ParticipantID")]
public virtual Participant Participant { get; set; }
public int MeetingID { get; set; }
[ForeignKey("MeetingID")]
public virtual Meeting Meeting { get; set; }
}
并更新参与者和会议 class 如下:
public record Participant(int id)
{
public string name { get; set; } = default!;
public string title { get; set; } = default!;
public List<MeetingParticipant> MeetingParticipant { get; set; }
}
public record Meeting(int id)
{
public string subject { get; set; } = default!;
public string organizer { get; set; } = default!;
public DateTime starttime { get; set; } = default!;
public DateTime endtime { get; set; } = default!;
public List<MeetingParticipant> MeetingParticipant { get; set; }
}
然后,更新 ApplicationDbContext(您可以将其更改为您的),如下所示:
public class ApplicationDbContext : IdentityDbContext
{
public DbSet<Participant> Participants => Set<Participant>();
public DbSet<Meeting> Meetings => Set<Meeting>();
public DbSet<MeetingParticipant> MeetingParticipant { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//set the primary key
modelBuilder.Entity<MeetingParticipant> ().HasKey(mp=> new { mp.MeetingID, mp.ParticipantID});
}
然后,使用 EF 迁移命令启用迁移,如下所示(如果使用 Visual Studio):
Add-Migration AddMeetingParticipant
Update-Database
然后,它将在Participant和Meeting table之间配置many-to-many,MeetingParticipant table用于存储关系。
更多关于EF核心关系的详细信息,请参考以下文章:
Configure Many-to-Many Relationships in Entity Framework Core
我有两个 table,Participant
和 Meeting
,在一个 ASP.NET Core 6 minimal API 项目中设置如下:
record Participant(int id)
{
public string name { get; set; } = default!;
public string title { get; set; } = default!;
}
record Meeting(int id)
{
public string subject { get; set; } = default!;
public string organizer { get; set; } = default!;
public DateTime starttime { get; set; } = default!;
public DateTime endtime { get; set; } = default!;
}
class MyDb: DbContext
{
public MyDb(DbContextOptions<MyDb> options): base(options)
{
}
public DbSet<Participant> Participants => Set<Participant>();
public DbSet<Meeting> Meetings => Set<Meeting>();
}
现在我需要创建一个组合 table/记录来连接这两个 tables/记录
在 SQL 中就是这样 table
CREATE TABLE meetings_participants
(
meeting_id int NOT NULL,
participant_id int NOT NULL,
PRIMARY KEY (meeting_id, participant_id),
CONSTRAINT fk_meeting_id
FOREIGN KEY (meeting_id) REFERENCES meetings(id),
CONSTRAINT fk_participant_id
FOREIGN KEY (participant_id) REFERENCES participants(id)
);
但是我如何在 EF 中将它写为带有外键的记录?
您可以像这样创建一个 MeetingParticipant class:
public class MeetingParticipant
{
public int ParticipantID { get; set; }
[ForeignKey("ParticipantID")]
public virtual Participant Participant { get; set; }
public int MeetingID { get; set; }
[ForeignKey("MeetingID")]
public virtual Meeting Meeting { get; set; }
}
并更新参与者和会议 class 如下:
public record Participant(int id)
{
public string name { get; set; } = default!;
public string title { get; set; } = default!;
public List<MeetingParticipant> MeetingParticipant { get; set; }
}
public record Meeting(int id)
{
public string subject { get; set; } = default!;
public string organizer { get; set; } = default!;
public DateTime starttime { get; set; } = default!;
public DateTime endtime { get; set; } = default!;
public List<MeetingParticipant> MeetingParticipant { get; set; }
}
然后,更新 ApplicationDbContext(您可以将其更改为您的),如下所示:
public class ApplicationDbContext : IdentityDbContext
{
public DbSet<Participant> Participants => Set<Participant>();
public DbSet<Meeting> Meetings => Set<Meeting>();
public DbSet<MeetingParticipant> MeetingParticipant { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//set the primary key
modelBuilder.Entity<MeetingParticipant> ().HasKey(mp=> new { mp.MeetingID, mp.ParticipantID});
}
然后,使用 EF 迁移命令启用迁移,如下所示(如果使用 Visual Studio):
Add-Migration AddMeetingParticipant
Update-Database
然后,它将在Participant和Meeting table之间配置many-to-many,MeetingParticipant table用于存储关系。
更多关于EF核心关系的详细信息,请参考以下文章:
Configure Many-to-Many Relationships in Entity Framework Core