【问题标题】:Getting Invalid column name 'EmploymentTypeEntityEmploymentTypeID in Entity framework core在实体框架核心中获取无效的列名'EmploymentTypeEntityEmploymentTypeID
【发布时间】:2021-08-17 04:16:46
【问题描述】:

我收到以下错误。

消息: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
        }
    }

【问题讨论】:

  • 问题出在就业类型组实体类中。请张贴代码。
  • 按要求更新

标签: c# entity-framework-core


【解决方案1】:

您正在创建EmploymentTypeGroupEntityEmploymentTypeEntity 之间的关系。但是您并没有告诉实体框架这种关系是什么。 EF 猜测您想要在 EmploymentTypeGroupEntity 表中引用 EmploymentTypeEntity 并为此创建了一个字段。这显然不是你想要的。

你需要告诉 EF 是什么关系。如果是一对多关系,其中一个EmploymentTypeEntity 可以有多个EmploymentTypeGroupEntity,这似乎是因为您已经定义了:

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

您还需要在 EmploymentTypeGroupEntity 类中创建一个外键。所以添加到这个类:

public int EmploymentTypeEntityID { get; set; }

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

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

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

添加构造函数以将new List&lt;EmploymentTypeGroupEntity&gt;() 分配给EmploymentTypeGroups

更改测试中的数组分配以添加到集合中,并将外键添加到组创建中。

【讨论】:

  • 已解决。但是有一个疑问为什么要使用 ICollection?
  • 我不知道。但是我看到的每个微软的例子都使用这种类型。反向 POCO 创建者脚本也会创建 ICollections。我从未见过使用其他集合类型。
  • 您知道如何在执行第一个代码时为多个表设置 IDENTITY_INSERT。
  • 谢谢史密斯,但我想在同一个会话中为多个表做
  • 为什么要使用 ICollection? -- 因为它是允许添加/删除项目的最少指定集合接口,是 CRUD 中的标准操作。
猜你喜欢
  • 2022-01-09
  • 2019-08-21
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
相关资源
最近更新 更多