簡體   English   中英

在實體框架核心中獲取無效的列名稱“EmploymentTypeEntityEmploymentTypeID”

[英]Getting Invalid column name 'EmploymentTypeEntityEmploymentTypeID in Entity framework core

我收到以下錯誤。

消息:Microsoft.EntityFrameworkCore.DbUpdateException:更新條目時發生錯誤。 有關詳細信息,請參閱內部異常 ----> System.Data.SqlClient.SqlException:無效的列名稱“EmploymentTypeEntityEmploymentTypeID”。

奇怪的是它將我的實體類名稱和實體屬性名稱組合在一起。

以下是我的代碼。

SystemTest.cs

   using (var transaction = _referenceDataDbContext.Database.BeginTransaction())
            {
                _referenceDataDbContext.EmploymentType.AddRangeAsync(

                    new EmploymentTypeEntity
                    {
                        EmploymentTypeID = 1,
                        EmploymentType = "EmploymentType",
                        CategoryTypeID = 27,

                        SiteAddress = null,
                        CreatedBy = "UnitTest",
                        CreatedOn = DateTime.Now,
                        ModifiedBy = "UnitTest",
                        ModifiedOn = DateTime.Now,
                        RowVersion = new RowVersion(1),
                        EmploymentTypeGroups = new[]
                        {
                        new EmploymentTypeGroupEntity
                        {
                            EmploymentTypeGroupID = 11, GroupName = "Child Care", IsActive = true
                        }
                        }
                    }

                    }
                );

                _referenceDataDbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [ref].[EmploymentType] ON");

                _referenceDataDbContext.SaveChanges();
            }

EmploymentTypeGroup.cs

  public class EmploymentTypeGroupEntity
    {
        [Key]
        public int? EmploymentTypeGroupID { get; set; }
        public string GroupName { get; set; }
        public bool? IsActive { get; set; }
        public DateTime? CreatedOn { get; set; }
        public string CreatedBy { get; set; }
        public DateTime? ModifiedOn { get; set; }
        public string ModifiedBy { get; set; }

        [Timestamp]
        [ConcurrencyCheck]
        public byte[] RowVersion { get; set; }

    }

EmploymentType.cs

public class EmploymentTypeEntity
    {
        [Key]
        public int? EmploymentTypeID { get; set; }
        public string EmploymentType { get; set; }
        public int? CategoryTypeID { get; set; }

        public bool? SiteAddress { get; set; }
        public IEnumerable<EmploymentTypeGroupEntity> EmploymentTypeGroups { get; set; }
        public DateTime? CreatedOn { get; set; }
        public string CreatedBy { get; set; }
        public DateTime? ModifiedOn { get; set; }
        public string ModifiedBy { get; set; }

        [Timestamp]
        [ConcurrencyCheck]
        public byte[] RowVersion { get; set; }
    }

DataDbContext.cs

public class ReferenceDataDbContext : DbContext
    {
        public ReferenceDataDbContext(DbContextOptions<ReferenceDataDbContext> options)
            : base(options)
        {
        }

        public ReferenceDataDbContext()
        {

        }


        public virtual DbSet<EmploymentTypeEntity> EmploymentType { get; set; }
        public virtual DbSet<EmploymentTypeGroupEntity> EmploymentTypeGroup { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<StateEntity>().ToTable("State", "ref");
            builder.Entity<EmploymentTypeGroupEntity>().ToTable("EmploymentTypeGroup", "ref");
            builder.Entity<EmploymentTypeEntity>().ToTable("EmploymentType","ref").HasMany(a => a.EmploymentTypeGroups);

            // Configure database attributes
        }
    }

您正在創建EmploymentTypeGroupEntity和EmploymentTypeEntity之間的關系。 但是你並沒有告訴Entity Framework這種關系是什么。 EF已經猜到你想在EmploymentTypeGroupEntity表中引用EmploymentTypeEntity並為其創建一個字段。 這顯然不是你想要的。

你需要告訴EF這種關系是什么。 如果它是一對多的關系,其中一個EmploymentTypeEntity可以有許多EmploymentTypeGroupEntity,這似乎是因為你已定義:

public IEnumerable<EmploymentTypeGroupEntity> EmploymentTypeGroups { get; set; }

您還需要在EmploymentTypeGroupEntity類中創建外鍵。 所以添加到這個類:

public int EmploymentTypeEntityID { get; set; }

[ForeignKey(EmploymentTypeEntityID)]
public EmploymentTypeEntity EmploymentTypeEntity  { get; set; }

在您的EmploymentTypeEntity類中更改集合類型:

public ICollection<EmploymentTypeGroupEntity> EmploymentTypeGroups { get; set; }

添加構造函數以將新List()分配給EmploymentTypeGroups。

更改測試中的數組分配以添加到集合,並將外鍵添加到組創建中。

暫無
暫無

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

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