gpt4 book ai didi

c# - EF7 beta5 : Error trying to insert child entity

转载 作者:太空宇宙 更新时间:2023-11-03 15:37:39 24 4
gpt4 key购买 nike

我使用 ASP.NET5 和 Entity Framework 7.0.0-beta 5 创建了一个 API。

我按照描述创建了模型、DbContext 和存储库 here ,当我尝试使用子实体插入数据时,我的应用程序抛出异常:

“无法将 NULL 值插入表‘公司’的‘状态’列;该列不允许空值。插入失败。”

Code + Exception

我的 Company 实体中的 Status 属性不为空,但我不知道为什么 EF 无法插入子实体的值。 (在本例中为“1”)

这是堆栈跟踪:

   at Microsoft.Data.Entity.Relational.Update.ReaderModificationCommandBatch.Execute(RelationalTransaction transaction, IRelationalTypeMapper typeMapper, DbContext context, ILogger logger)
at Microsoft.Data.Entity.Relational.Update.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
at Microsoft.Data.Entity.Relational.RelationalDataStore.SaveChanges(IReadOnlyList`1 entries)
at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.Data.Entity.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.Data.Entity.DbContext.SaveChanges()
at **.Data.Repositories.CompanyRepository.Create(Company company) in D:\**.Data\Repositories\CompanyRepository.cs:line 36
at **.API.Controllers.CompanyController.Create(Company company) in D:\**.API\Controllers\CompanyController.cs:line 48

代码如下:

数据库上下文

public class MrBellhopContext : DbContext
{

public DbSet<Company> Company { get; set; }
public DbSet<CompanyStatus> CompanyStatus { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>().Table("Company");
modelBuilder.Entity<Company>().Key(c => c.CompanyId);
modelBuilder.Entity<Company>().Index(c => c.Name);
modelBuilder.Entity<Company>().Reference(c => c.Status).InverseReference().ForeignKey<CompanyStatus>(cs => cs.StatusId);

modelBuilder.Entity<CompanyStatus>().Table("CompanyStatus");
modelBuilder.Entity<CompanyStatus>().Key(cs => cs.StatusId);

base.OnModelCreating(modelBuilder);
}

}

型号

public class Company
{

public int CompanyId { get; set; }

public string Name { get; set; }

public string Email { get; set; }

public CompanyStatus Status { get; set; }

public DateTime CreatedAt { get; set; }

public DateTime UpdatedAt { get; set; }

}

public class CompanyStatus
{

public int StatusId { get; set; }

public string Name { get; set; }

}

Repository(仅创建方法)

    public int Create(Company company)
{
_dbcontext.Add(company);
_dbcontext.SaveChanges();

return company.CompanyId;
}

有谁知道如何正确插入子实体?

最佳答案

尝试将 Repository 方法更改为:

public int Create(Company company)
{
using (MyDbcontextdbcontext context = new MyDbcontext())
{
context.Entry(company).State = EntityState.Added;
context.SaveChanges();
}
}

更多信息:https://msdn.microsoft.com/en-us/data/jj592676.aspx

关于c# - EF7 beta5 : Error trying to insert child entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31301688/

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