Я получаю приведенную ниже ошибку.
Сообщение: 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
}
}