[英]Can not insert Data to table with a foreign key to a IdentityColumn, in ef core 2
I can not insert Data to a sql table with a foreign key to a IdentityColumn, in ef core 2. 我无法在ef core 2中使用对IdentityColumn的外键将数据插入到sql表中。
I have the following Entitie classes: 我有以下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; }
}
I map the classes to two SQL-Server tables with the Fluent API: 我使用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});
});
The Tables: 表格:
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
)
When I try to add CourseMeetingParticipant to the table CourseMeetingParticipants I get the following Error: 当我尝试将CourseMeetingParticipant添加到表CourseMeetingParticipant时,出现以下错误:
InvalidOperationException: The property 'MID' on entity type 'CourseMeetingParticipant' has a temporary value. InvalidOperationException:实体类型“ CourseMeetingParticipant”上的属性“ MID”具有临时值。 Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.
显式设置一个永久值,或者确保将数据库配置为为此属性生成值。
This is the method I use to add the CourseMeetingParticipant: 这是我用来添加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");
}
If i remove the HasOne, WithOne relations between the two entities i can add the CourseMeetingParticipant to the table with no problems. 如果删除两个实体之间的HasOne,WithOne关系,则可以毫无问题地将CourseMeetingParticipant添加到表中。 But in need the relations in other part of my Application.
但是需要我应用程序其他部分的关系。 Have you any idea why this is not working?
您知道为什么这行不通吗?
Configuration: ASP.NET Core 2.1 配置:ASP.NET Core 2.1
You already have the relationship for CourseMeeting
<--> CourseMeetingParticipant
defined in your CourseMeeting
entity. 您已经具有在
CourseMeeting
实体中定义的CourseMeeting
<-> CourseMeetingParticipant
的关系。 What if you remove the redundant definition from CourseMeetingParticipant
? 如果您从
CourseMeetingParticipant
删除多余的定义CourseMeetingParticipant
办?
modelBuilder.Entity<CourseMeetingParticipant>(c =>
{
c.Property(p => p.MID);
c.HasKey(k => new {k.MID, k.SUID});
});
It may also make sense for you to make CourseMeetingParticipants.MID
non-nullable. 使
CourseMeetingParticipants.MID
不可为空对您来说也很有意义。 Then your table would look like: 然后您的表将如下所示:
CREATE TABLE [dbo].[CourseMeetingParticipants](
[MID] [int] NOT NULL,
[SUID] [varchar](100) NOT NULL
) ON [PRIMARY]
and your CourseMeeting
entity would have a required property: 并且您的
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.