gpt4 book ai didi

c# - 在 Entity Framework 核心中获取无效的列名称'EmploymentTypeEntityEmploymentTypeID

转载 作者:行者123 更新时间:2023-11-30 17:28:37 25 4
gpt4 key购买 nike

我收到以下错误。

消息: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 已经猜到您想要对 EmploymentTypeEntity 的引用在你的EmploymentTypeGroupEntity表并为此创建了一个字段。这显然不是您想要的。

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

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

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

public int EmploymentTypeEntityID { get; set; }

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

在你的EmploymentTypeEntity类更改集合类型:

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

添加一个构造函数来分配一个new List<EmploymentTypeGroupEntity>()EmploymentTypeGroups .

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

关于c# - 在 Entity Framework 核心中获取无效的列名称'EmploymentTypeEntityEmploymentTypeID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52526786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com