繁体   English   中英

在ef core 2中,无法使用IdentityId的外键将数据插入表

[英]Can not insert Data to table with a foreign key to a IdentityColumn, in ef core 2

问题

我无法在ef core 2中使用对IdentityColumn的外键将数据插入到sql表中。

设定:

我有以下Entitie课程:

public class CourseMeeting
{
   public int MID{ get; set; }
   public int CID { get; set; }
   public Course Course { get; set; }
   public DateTime DateOfTheMeeting { get; set; }  
   public int MaxParticipants { get; set; }
   public IList<CourseMeetingParticipant> CourseMeetingParticipants {get;set;}
   }

public class CourseMeetingParticipant
{
   public int MID{ get; set; }
   public CourseMeeting CourseMeeting { get; set; }
   public string SUID { get; set; }
   public User Student { get; set; }
}

我使用Fluent API将类映射到两个SQL-Server表:

modelBuilder.Entity<CourseMeeting>(cm =>
{
    cm.HasKey(k => k.MID);

    cm.Property(p => p.MID)
        .UseSqlServerIdentityColumn();

    cm.HasMany(m => m.CourseMeetingParticipants)
        .WithOne(m => m.CourseMeeting);
});


modelBuilder.Entity<CourseMeetingParticipant>(c =>
{

c.HasOne(m => m.CourseMeeting)
      .WithMany(m => m.CourseMeetingParticipants)
      .HasForeignKey(k => k.MID)
      .HasPrincipalKey(p => p.MID);

c.Property(p => p.MID);

c.HasKey(k => new {k.MID, k.SUID});
});

表格:

CREATE TABLE [dbo].[CourseMeetingParticipants](
    [MID] [int] NULL,
    [SUID] [varchar](100) NOT NULL
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[CourseMeetingParticipants]  WITH CHECK ADD FOREIGN KEY([MID])
REFERENCES [dbo].[CourseMeetings] ([MID])

alter table [dbo].[CourseMeetingParticipants]
ADD CONSTRAINT PK_CP PRIMARY KEY CLUSTERED (MID, SUID);  

go
CREATE TABLE [dbo].[CourseMeetings]
(
    [MID] INT IDENTITY(0,1) PRIMARY KEY
   ,[CID] INT FOREIGN KEY REFERENCES Courses (CID)
   ,[DateOfTheMeeting] datetime2 not null
   ,[MaxParticipants] int not null
)

当我尝试将CourseMeetingParticipant添加到表CourseMeetingParticipant时,出现以下错误:

InvalidOperationException:实体类型“ CourseMeetingParticipant”上的属性“ MID”具有临时值。 显式设置一个永久值,或者确保将数据库配置为为此属性生成值。

这是我用来添加CourseMeetingParticipant的方法:

[HttpPost]
[Authorize(Roles = "Student")]
public async Task<IActionResult> SignInToCourseMeeting(int id){
    var participant = new CourseMeetingParticipant{
        MID = id,
        SUID = await _UserManger.GetUserIdAsync(await _UserManger.GetUserAsync(User)),
    };
    _db.CourseMeetingParticipants.Add(participant);
    await _db.SaveChangesAsync();
    return Redirect("/Course/Courses");
 }

如果删除两个实体之间的HasOne,WithOne关系,则可以毫无问题地将CourseMeetingParticipant添加到表中。 但是需要我应用程序其他部分的关系。 您知道为什么这行不通吗?

配置:ASP.NET Core 2.1

您已经具有在CourseMeeting实体中定义的CourseMeeting <-> CourseMeetingParticipant的关系。 如果您从CourseMeetingParticipant删除多余的定义CourseMeetingParticipant办?

modelBuilder.Entity<CourseMeetingParticipant>(c =>
{
    c.Property(p => p.MID);
    c.HasKey(k => new {k.MID, k.SUID});
});

使CourseMeetingParticipants.MID不可为空对您来说也很有意义。 然后您的表将如下所示:

CREATE TABLE [dbo].[CourseMeetingParticipants](
    [MID] [int] NOT NULL,
    [SUID] [varchar](100) NOT NULL
) ON [PRIMARY]

并且您的CourseMeeting实体将具有必需的属性:

modelBuilder.Entity<CourseMeeting>(cm =>
{
    cm.HasKey(k => k.MID);

    cm.Property(p => p.MID)
        .UseSqlServerIdentityColumn();

    cm.HasMany(m => m.CourseMeetingParticipants)
        .WithRequired(m => m.CourseMeeting);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM