簡體   English   中英

ASP.net 中的實體框架

[英]Entity Framework in ASP.net

我正在為實體框架使用數據庫優先方法。 下面是我的桌子。 我的表中有一個標識列,但是當創建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);
}

當我嘗試使用下面的代碼將數據保存到數據庫時,我收到了這個錯誤:

無法跟蹤“IdentityPersonalData”類型的實例,因為它沒有主鍵。 只能跟蹤具有主鍵的實體類型。

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創建的。

當您添加到 model 構建器時,您已明確指定 model 根本沒有密鑰

entity.HasNoKey();

刪除並添加

e.HasKey(s => s.RecordId);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM