简体   繁体   English

在实体框架核心中获取无效的列名称“EmploymentTypeEntityEmploymentTypeID”

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

I am getting the below error. 我收到以下错误。

Message: Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. 消息:Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时发生错误。 See the inner exception for details. 有关详细信息,请参阅内部异常 ----> System.Data.SqlClient.SqlException : Invalid column name 'EmploymentTypeEntityEmploymentTypeID'. ----> System.Data.SqlClient.SqlException:无效的列名称“EmploymentTypeEntityEmploymentTypeID”。

Its strange as its combining my Entity Class Name and the Entity Property Name. 奇怪的是它将我的实体类名称和实体属性名称组合在一起。

Below is my code. 以下是我的代码。

SystemTest.cs 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 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 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 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
        }
    }

You are creating a relationship between EmploymentTypeGroupEntity and EmploymentTypeEntity. 您正在创建EmploymentTypeGroupEntity和EmploymentTypeEntity之间的关系。 But you are not telling Entity Framework what that relationship is. 但是你并没有告诉Entity Framework这种关系是什么。 EF has guessed that you want a reference to EmploymentTypeEntity in your EmploymentTypeGroupEntity table and created a field for that. EF已经猜到你想在EmploymentTypeGroupEntity表中引用EmploymentTypeEntity并为其创建一个字段。 This clearly isn't what you want. 这显然不是你想要的。

You need to tell EF what the relationship is. 你需要告诉EF这种关系是什么。 If it is a one-many relationship where one EmploymentTypeEntity can have many EmploymentTypeGroupEntity's, which appears to be the case because you have defined: 如果它是一对多的关系,其中一个EmploymentTypeEntity可以有许多EmploymentTypeGroupEntity,这似乎是因为你已定义:

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

You also need to create a foreign key in your EmploymentTypeGroupEntity class. 您还需要在EmploymentTypeGroupEntity类中创建外键。 so add to this class: 所以添加到这个类:

public int EmploymentTypeEntityID { get; set; }

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

In your EmploymentTypeEntity class change the collection type: 在您的EmploymentTypeEntity类中更改集合类型:

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

Add a constructor to assign a new List() to EmploymentTypeGroups. 添加构造函数以将新List()分配给EmploymentTypeGroups。

Change the array assignment in your tests to add to the collection and add the foreign key to the groups creation. 更改测试中的数组分配以添加到集合,并将外键添加到组创建中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM