Entity Framework 在 ASP.net
Entity Framework in ASP.net
我正在为 Entity Framework 使用数据库优先的方法。下面是我的table。我的 table 中有一个标识列,但是当创建 dbcontext
class 时,它在 OnModelCreating
方法中有 entity.HasNoKey
。
下面是自动生成的代码:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityPersonalData>(entity =>
{
entity.HasNoKey();
entity.ToTable("Identity_PersonalData");
entity.Property(e => e.Address1)
.HasMaxLength(100)
.IsUnicode(false);
entity.Property(e => e.Address2)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.City)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.Email)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.FirstName)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.LastName)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.MiddleName)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.PhoneNumber)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.RecordId).ValueGeneratedOnAdd();
entity.Property(e => e.RequestType)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.State)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.Status)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.Zip)
.HasMaxLength(50)
.IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
当我尝试使用以下代码将数据保存到数据库时,出现此错误:
Unable to track an instance of type 'IdentityPersonalData' because it does not have a primary key. Only entity types with primary keys may be tracked.
public void SaveData(IdentityPersonalData objPerson)
{
using (IdentityProofContext IdContext = new IdentityProofContext())
{
IdContext.IdentityPersonalData.Add(objPerson);
IdContext.SaveChanges();
Int64 id = objPerson.RecordId;
}
}
这是我的 class:
public partial class IdentityPersonalData
{
[Key]
public Int64 RecordId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string RequestType { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Status { get; set; }
}
我在 recordId
上没有 [Key]
注释。当我收到上面提到的这个错误时,我稍后放置了关键注释。我不明白如果我已经有一个密钥为什么我会收到错误消息以及为什么上下文 class 是用 entity.HasNoKey
.
创建的
您在添加到模型构建器时已明确指定该模型根本没有密钥
entity.HasNoKey();
删除并添加
e.HasKey(s => s.RecordId);
我正在为 Entity Framework 使用数据库优先的方法。下面是我的table。我的 table 中有一个标识列,但是当创建 dbcontext
class 时,它在 OnModelCreating
方法中有 entity.HasNoKey
。
下面是自动生成的代码:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityPersonalData>(entity =>
{
entity.HasNoKey();
entity.ToTable("Identity_PersonalData");
entity.Property(e => e.Address1)
.HasMaxLength(100)
.IsUnicode(false);
entity.Property(e => e.Address2)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.City)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.Email)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.FirstName)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.LastName)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.MiddleName)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.PhoneNumber)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.RecordId).ValueGeneratedOnAdd();
entity.Property(e => e.RequestType)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.State)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.Status)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.Zip)
.HasMaxLength(50)
.IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
当我尝试使用以下代码将数据保存到数据库时,出现此错误:
Unable to track an instance of type 'IdentityPersonalData' because it does not have a primary key. Only entity types with primary keys may be tracked.
public void SaveData(IdentityPersonalData objPerson)
{
using (IdentityProofContext IdContext = new IdentityProofContext())
{
IdContext.IdentityPersonalData.Add(objPerson);
IdContext.SaveChanges();
Int64 id = objPerson.RecordId;
}
}
这是我的 class:
public partial class IdentityPersonalData
{
[Key]
public Int64 RecordId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string RequestType { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Status { get; set; }
}
我在 recordId
上没有 [Key]
注释。当我收到上面提到的这个错误时,我稍后放置了关键注释。我不明白如果我已经有一个密钥为什么我会收到错误消息以及为什么上下文 class 是用 entity.HasNoKey
.
您在添加到模型构建器时已明确指定该模型根本没有密钥
entity.HasNoKey();
删除并添加
e.HasKey(s => s.RecordId);