我有以下模型,一个ImprovementNotice
,其中可以包含ImprovementNoticeFiles
的列表:
public class ImprovementNotice
{
public ImprovementNotice() {
this.ImprovementNoticeFiles = new List<ImprovementNoticeFile>();
}
[Key]
public int ImprovementNoticeId { get; set; }
public List<ImprovementNoticeFile> ImprovementNoticeFiles {get; set;}
}
public class ImprovementNoticeFile
{
[Key]
public int ImprovementNoticeFileId { get; set; }
[MaxLength(128)]
public string Filename { get; set; }
public int ImprovementNotice_ImprovementNoticeId { get; set; }
[JsonIgnore]
[ForeignKey("ImprovementNotice_ImprovementNoticeId")]
public virtual ImprovementNotice ImprovementNotice { get; set; }
}
我的上下文定义:
public virtual DbSet<ImprovementNotice> ImprovementNotices { get; set; }
public virtual DbSet<ImprovementNoticeFile> ImprovementNoticeFiles { get; set; }
在SaveChanges
命令上更新无效对象名称'ImprovementNoticeFile'时出现错误。
正在生成的SQL:
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (277ms) [Parameters=[@p0='filename.pdf' (Size = 128), @p1='1010'], CommandType='Text', CommandTimeout='30']
SET NOCOUNT ON;
INSERT INTO [ImprovementNoticeFile] ([Filename], [ImprovementNotice_ImprovementNoticeId])
VALUES (@p0, @p1);
SELECT [ImprovementNoticeFileId]
FROM [ImprovementNoticeFile]
WHERE @@ROWCOUNT = 1 AND [ImprovementNoticeFileId] = scope_identity();
表名应为ImprovementNoticeFiles
,末尾带有s
,但是生成的SQL试图插入到ImprovementNoticeFile
中。
更新
这是创建表的代码首次迁移:
CreateTable(
"dbo.ImprovementNoticeFiles",
c => new
{
ImprovementNoticeFileId = c.Int(nullable: false, identity: true),
Filename = c.String(maxLength: 128),
ImprovementNotice_ImprovementNoticeId = c.Int(nullable: false),
})
.PrimaryKey(t => t.ImprovementNoticeFileId)
.ForeignKey("dbo.ImprovementNotices", t => t.ImprovementNotice_ImprovementNoticeId, cascadeDelete: true)
.Index(t => t.ImprovementNotice_ImprovementNoticeId);