我首先遇到了 EF 代码的问题
当我尝试将新记录插入表中时,我收到消息。
Cannot insert explicit value for identity column in table '' when IDENTITY_INSERT is set to OFF.
知道为什么我会收到错误吗?
我正在使用以下代码:
public void SaveNewResponse()
{
using (var context = new Context())
{
var newResponse = new Response()
{
lngRequestLineID = 1001233,
memXMLResponse = "test Response",
fAdhoc = false,
};
context.tblResponses.Add(newResponse);
context.SaveChanges();
}
}
这是我的映射
public class tblResponsMap : EntityTypeConfiguration<tblRespons>
{
public tblResponsMap()
{
// Primary Key
this.HasKey(t => new { t.lngResponseLineID});
// Properties
this.Property(t => t.lngResponseLineID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.lngRequestLineID);
// Table & Column Mappings
this.ToTable("tblResponses");
this.Property(t => t.lngResponseLineID).HasColumnName("lngResponseLineID");
this.Property(t => t.lngRequestLineID).HasColumnName("lngRequestLineID");
this.Property(t => t.fAdhoc).HasColumnName("fAdhoc");
this.Property(t => t.memXMLResponse).HasColumnName("memXMLResponse");
}
}