[英]Entity Framework Core 2.1 failling to update entities with relations
我目前遇到EF核心2.1和本機客戶端用來更新包含多層嵌入對象的對象的web api的問題。 我已經閱讀了這兩個主題:
https://docs.microsoft.com/en-us/ef/core/saving/disconnected-entities
我已經了解到,現在更新EF Core 2中的對象確實不是那么明顯。但我還沒有設法找到一個有效的解決方案。 在每次嘗試時,我都有一個例外,告訴我EF已經跟蹤了“步驟”。
我的模型看起來像這樣:
//CIApplication the root class I’m trying to update
public class CIApplication : ConfigurationItem // -> derive of BaseEntity which holds the ID and some other properties
{
//Collection of DeploymentScenario
public virtual ICollection<DeploymentScenario> DeploymentScenarios { get; set; }
//Collection of SoftwareMeteringRules
public virtual ICollection<SoftwareMeteringRule> SoftwareMeteringRules { get; set; }
}
//與應用程序具有一對多關系的部署方案。 部署方案包含兩個步驟列表
public class DeploymentScenario : BaseEntity
{
//Collection of substeps
public virtual ICollection<Step> InstallSteps { get; set; }
public virtual ICollection<Step> UninstallSteps { get; set; }
//Navigation properties Parent CI
public Guid? ParentCIID { get; set; }
public virtual CIApplication ParentCI { get; set; }
}
//步驟,這也很復雜,也是自引用的
public class Step : BaseEntity
{
public string ScriptBlock { get; set; }
//Parent Step Navigation property
public Guid? ParentStepID { get; set; }
public virtual Step ParentStep { get; set; }
//Parent InstallDeploymentScenario Navigation property
public Guid? ParentInstallDeploymentScenarioID { get; set; }
public virtual DeploymentScenario ParentInstallDeploymentScenario { get; set; }
//Parent InstallDeploymentScenario Navigation property
public Guid? ParentUninstallDeploymentScenarioID { get; set; }
public virtual DeploymentScenario ParentUninstallDeploymentScenario { get; set; }
//Collection of sub steps
public virtual ICollection<Step> SubSteps { get; set; }
//Collection of input variables
public virtual List<ScriptVariable> InputVariables { get; set; }
//Collection of output variables
public virtual List<ScriptVariable> OutPutVariables { get; set; }
}
這是我的更新方法,我知道它很難看,它不應該在控制器中,但我每兩個小時就更換一次,因為我試圖在網上找到解決方案。 所以這是來自https://docs.microsoft.com/en-us/ef/core/saving/disconnected-entities的最后一次迭代
public async Task<IActionResult> PutCIApplication([FromRoute] Guid id, [FromBody] CIApplication cIApplication)
{
_logger.LogWarning("Updating CIApplication " + cIApplication.Name);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != cIApplication.ID)
{
return BadRequest();
}
var cIApplicationInDB = _context.CIApplications
.Include(c => c.Translations)
.Include(c => c.DeploymentScenarios).ThenInclude(d => d.InstallSteps).ThenInclude(s => s.SubSteps)
.Include(c => c.DeploymentScenarios).ThenInclude(d => d.UninstallSteps).ThenInclude(s => s.SubSteps)
.Include(c => c.SoftwareMeteringRules)
.Include(c => c.Catalogs)
.Include(c => c.Categories)
.Include(c => c.OwnerCompany)
.SingleOrDefault(c => c.ID == id);
_context.Entry(cIApplicationInDB).CurrentValues.SetValues(cIApplication);
foreach(var ds in cIApplication.DeploymentScenarios)
{
var existingDeploymentScenario = cIApplicationInDB.DeploymentScenarios.FirstOrDefault(d => d.ID == ds.ID);
if (existingDeploymentScenario == null)
{
cIApplicationInDB.DeploymentScenarios.Add(ds);
}
else
{
_context.Entry(existingDeploymentScenario).CurrentValues.SetValues(ds);
foreach(var step in existingDeploymentScenario.InstallSteps)
{
var existingStep = existingDeploymentScenario.InstallSteps.FirstOrDefault(s => s.ID == step.ID);
if (existingStep == null)
{
existingDeploymentScenario.InstallSteps.Add(step);
}
else
{
_context.Entry(existingStep).CurrentValues.SetValues(step);
}
}
}
}
foreach(var ds in cIApplicationInDB.DeploymentScenarios)
{
if(!cIApplication.DeploymentScenarios.Any(d => d.ID == ds.ID))
{
_context.Remove(ds);
}
}
//_context.Update(cIApplication);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException e)
{
if (!CIApplicationExists(id))
{
return NotFound();
}
else
{
throw;
}
}
catch(Exception e)
{
}
return Ok(cIApplication);
}
到目前為止,我遇到了這個異常:無法跟蹤實體類型“Step”的實例,因為已經跟蹤了另一個鍵值為“{ID:e29b3c1c-2e06-4c7b-b0cd-f8f1c5ccb7b6}”的實例。
我注意到客戶以前沒有做過“獲取”操作,即使是這樣我也把AsNoTracking放在我的get方法上。 客戶端在更新之前進行的唯一操作是“_context.CIApplications.Any(e => e.ID == id);”如果我應該添加新記錄或更新現有記錄。
幾天以來我一直在與這個問題作斗爭,所以如果有人能幫助我朝着正確的方向前進,我真的很感激。 非常感謝
更新:
我在控制器中添加了以下代碼:
var existingStep = existingDeploymentScenario.InstallSteps.FirstOrDefault(s => s.ID == step.ID);
entries = _context.ChangeTracker.Entries();
if (existingStep == null)
{
existingDeploymentScenario.InstallSteps.Add(step);
entries = _context.ChangeTracker.Entries();
}
entries = _context.ChangeTracker.Entries(); line添加包含新步驟的新deploymentScenario后立即引發“已經跟蹤的步驟”異常。
就在它之前,新的deploymentScenario和步驟不在跟蹤器中,而且我已經在數據庫中檢查了他們的ID沒有重復。
我也檢查了我的Post方法,現在它也失敗了...我將它恢復為默認方法而沒有花哨的東西里面:
[HttpPost]
public async Task<IActionResult> PostCIApplication([FromBody] CIApplication cIApplication)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var entries = _context.ChangeTracker.Entries();
_context.CIApplications.Add(cIApplication);
entries = _context.ChangeTracker.Entries();
await _context.SaveChangesAsync();
entries = _context.ChangeTracker.Entries();
return CreatedAtAction("GetCIApplication", new { id = cIApplication.ID }, cIApplication);
}
條目在開頭是空的_context.CIApplications.Add(cIApplication); line仍然提出異常仍然是部署方案中包含的唯一一步......
所以當我嘗試在我的上下文中添加內容時顯然有些錯誤,但是現在我感覺完全迷失了。 這可能有助於我在啟動時聲明我的上下文:
services.AddDbContext<MyAppContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly("DeployFactoryDataModel")),
ServiceLifetime.Transient
);
添加我的上下文類:
public class MyAppContext : DbContext
{
private readonly IHttpContextAccessor _contextAccessor;
public MyAppContext(DbContextOptions<MyAppContext> options, IHttpContextAccessor contextAccessor) : base(options)
{
_contextAccessor = contextAccessor;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.EnableSensitiveDataLogging();
}
public DbSet<Step> Steps { get; set; }
//public DbSet<Sequence> Sequences { get; set; }
public DbSet<DeploymentScenario> DeploymentScenarios { get; set; }
public DbSet<ConfigurationItem> ConfigurationItems { get; set; }
public DbSet<CIApplication> CIApplications { get; set; }
public DbSet<SoftwareMeteringRule> SoftwareMeteringRules { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<ConfigurationItemCategory> ConfigurationItemsCategories { get; set; }
public DbSet<Company> Companies { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<Catalog> Catalogs { get; set; }
public DbSet<CIDriver> CIDrivers { get; set; }
public DbSet<DriverCompatiblityEntry> DriverCompatiblityEntries { get; set; }
public DbSet<ScriptVariable> ScriptVariables { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Step one to many with step for sub steps
modelBuilder.Entity<Step>().HasMany(s => s.SubSteps).WithOne(s => s.ParentStep).HasForeignKey(s => s.ParentStepID);
//Step one to many with step for variables
modelBuilder.Entity<Step>().HasMany(s => s.InputVariables).WithOne(s => s.ParentInputStep).HasForeignKey(s => s.ParentInputStepID);
modelBuilder.Entity<Step>().HasMany(s => s.OutPutVariables).WithOne(s => s.ParentOutputStep).HasForeignKey(s => s.ParentOutputStepID);
//Step one to many with sequence
//modelBuilder.Entity<Step>().HasOne(step => step.ParentSequence).WithMany(seq => seq.Steps).HasForeignKey(step => step.ParentSequenceID).OnDelete(DeleteBehavior.Cascade);
//DeploymentScenario One to many with install steps
modelBuilder.Entity<DeploymentScenario>().HasMany(d => d.InstallSteps).WithOne(s => s.ParentInstallDeploymentScenario).HasForeignKey(s => s.ParentInstallDeploymentScenarioID);
//DeploymentScenario One to many with uninstall steps
modelBuilder.Entity<DeploymentScenario>().HasMany(d => d.UninstallSteps).WithOne(s => s.ParentUninstallDeploymentScenario).HasForeignKey(s => s.ParentUninstallDeploymentScenarioID);
//DeploymentScenario one to one with sequences
//modelBuilder.Entity<DeploymentScenario>().HasOne(ds => ds.InstallSequence).WithOne(seq => seq.IDeploymentScenario).HasForeignKey<DeploymentScenario>(ds => ds.InstallSequenceID).OnDelete(DeleteBehavior.Cascade);
//modelBuilder.Entity<DeploymentScenario>().HasOne(ds => ds.UninstallSequence).WithOne(seq => seq.UDeploymentScenario).HasForeignKey<DeploymentScenario>(ds => ds.UninstallSequenceID);
//Step MUI config
modelBuilder.Entity<Step>().Ignore(s => s.Description);
modelBuilder.Entity<Step>().HasMany(s => s.Translations).WithOne().HasForeignKey(x => x.StepTranslationId);
//Sequence MUI config
//modelBuilder.Entity<Sequence>().Ignore(s => s.Description);
//modelBuilder.Entity<Sequence>().HasMany(s => s.Translations).WithOne().HasForeignKey(x => x.SequenceTranslationId);
//DeploymentScenario MUI config
modelBuilder.Entity<DeploymentScenario>().Ignore(s => s.Name);
modelBuilder.Entity<DeploymentScenario>().Ignore(s => s.Description);
modelBuilder.Entity<DeploymentScenario>().HasMany(s => s.Translations).WithOne().HasForeignKey(x => x.DeploymentScenarioTranslationId);
//CIApplication relations
//CIApplication one to many relation with Deployment Scenario
modelBuilder.Entity<CIApplication>().HasMany(ci => ci.DeploymentScenarios).WithOne(d => d.ParentCI).HasForeignKey(d => d.ParentCIID).OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<CIApplication>().HasMany(ci => ci.SoftwareMeteringRules).WithOne(d => d.ParentCI).HasForeignKey(d => d.ParentCIID).OnDelete(DeleteBehavior.Cascade);
// CIDriver relations
// CIAPpplication one to many relation with DriverCompatibilityEntry
modelBuilder.Entity<CIDriver>().HasMany(ci => ci.CompatibilityList).WithOne(c => c.ParentCI).HasForeignKey(c => c.ParentCIID).OnDelete(DeleteBehavior.Restrict);
//ConfigurationItem MUI config
modelBuilder.Entity<ConfigurationItem>().Ignore(s => s.Name);
modelBuilder.Entity<ConfigurationItem>().Ignore(s => s.Description);
modelBuilder.Entity<ConfigurationItem>().HasMany(s => s.Translations).WithOne().HasForeignKey(x => x.ConfigurationItemTranslationId);
//category MUI config
modelBuilder.Entity<Category>().Ignore(s => s.Name);
modelBuilder.Entity<Category>().Ignore(s => s.Description);
modelBuilder.Entity<Category>().HasMany(s => s.Translations).WithOne().HasForeignKey(x => x.CategoryTranslationId);
//CI Categories Many to Many
modelBuilder.Entity<ConfigurationItemCategory>().HasKey(cc => new { cc.CategoryId, cc.CIId });
modelBuilder.Entity<ConfigurationItemCategory>().HasOne(cc => cc.Category).WithMany(cat => cat.ConfigurationItems).HasForeignKey(cc => cc.CategoryId);
modelBuilder.Entity<ConfigurationItemCategory>().HasOne(cc => cc.ConfigurationItem).WithMany(ci => ci.Categories).HasForeignKey(cc => cc.CIId);
//CI Catalog Many to Many
modelBuilder.Entity<CICatalog>().HasKey(cc => new { cc.CatalogId, cc.ConfigurationItemId });
modelBuilder.Entity<CICatalog>().HasOne(cc => cc.Catalog).WithMany(cat => cat.CIs).HasForeignKey(cc => cc.CatalogId);
modelBuilder.Entity<CICatalog>().HasOne(cc => cc.ConfigurationItem).WithMany(ci => ci.Catalogs).HasForeignKey(cc => cc.ConfigurationItemId);
//Company Customers Many to Many
modelBuilder.Entity<CompanyCustomers>().HasKey(cc => new { cc.CustomerId, cc.ProviderId });
modelBuilder.Entity<CompanyCustomers>().HasOne(cc => cc.Provider).WithMany(p => p.Customers).HasForeignKey(cc => cc.ProviderId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<CompanyCustomers>().HasOne(cc => cc.Customer).WithMany(c => c.Providers).HasForeignKey(cc => cc.CustomerId);
//Company Catalog Many to Many
modelBuilder.Entity<CompanyCatalog>().HasKey(cc => new { cc.CatalogId, cc.CompanyId });
modelBuilder.Entity<CompanyCatalog>().HasOne(cc => cc.Catalog).WithMany(c => c.Companies).HasForeignKey(cc => cc.CatalogId);
modelBuilder.Entity<CompanyCatalog>().HasOne(cc => cc.Company).WithMany(c => c.Catalogs).HasForeignKey(cc => cc.CompanyId);
//Author Catalog Many to Many
modelBuilder.Entity<CatalogAuthors>().HasKey(ca => new { ca.AuthorId, ca.CatalogId });
modelBuilder.Entity<CatalogAuthors>().HasOne(ca => ca.Catalog).WithMany(c => c.Authors).HasForeignKey(ca => ca.CatalogId);
modelBuilder.Entity<CatalogAuthors>().HasOne(ca => ca.Author).WithMany(a => a.AuthoringCatalogs).HasForeignKey(ca => ca.AuthorId);
//Company one to many with owned Catalog
modelBuilder.Entity<Company>().HasMany(c => c.OwnedCatalogs).WithOne(c => c.OwnerCompany).HasForeignKey(c => c.OwnerCompanyID).OnDelete(DeleteBehavior.Restrict);
//Company one to many with owned Categories
modelBuilder.Entity<Company>().HasMany(c => c.OwnedCategories).WithOne(c => c.OwnerCompany).HasForeignKey(c => c.OwnerCompanyID).OnDelete(DeleteBehavior.Restrict);
//Company one to many with owned CIs
modelBuilder.Entity<Company>().HasMany(c => c.OwnedCIs).WithOne(c => c.OwnerCompany).HasForeignKey(c => c.OwnerCompanyID).OnDelete(DeleteBehavior.Restrict);
//CIDriver one to many with DriverCompatibilityEntry
modelBuilder.Entity<CIDriver>().HasMany(c => c.CompatibilityList).WithOne(c => c.ParentCI).HasForeignKey(c => c.ParentCIID).OnDelete(DeleteBehavior.Restrict);
//User Group Many to Many
modelBuilder.Entity<UserGroup>().HasKey(ug => new { ug.UserId, ug.GroupId });
modelBuilder.Entity<UserGroup>().HasOne(cg => cg.User).WithMany(ci => ci.Groups).HasForeignKey(cg => cg.UserId);
modelBuilder.Entity<UserGroup>().HasOne(cg => cg.Group).WithMany(ci => ci.Users).HasForeignKey(cg => cg.GroupId);
//User one to many with Company
modelBuilder.Entity<Company>().HasMany(c => c.Employees).WithOne(u => u.Employer).HasForeignKey(u => u.EmployerID).OnDelete(DeleteBehavior.Restrict);
}
更新2
這是一個指向minima repro示例的驅動器鏈接。 我沒有在客戶端實現PUT,因為post方法已經重現了這個問題。
您在此處列舉現有步驟,並搜索現有步驟集合中的現有步驟,這是沒有意義的。
foreach(var step in existingDeploymentScenario.InstallSteps)
var existingStep = existingDeploymentScenario.InstallSteps
.FirstOrDefault(s => s.ID == step.ID);
雖然應該是:
foreach(var step in ds.InstallSteps)
我想通了,我感到非常慚愧。
感謝大家,我終於懷疑客戶端和它處理數據的問題是由此引起的。
事實證明,當客戶端創建部署方案時,它會創建一個步驟並將其分配給installStep和uninstallSteps列表,從而導致問題...
我非常確定uninstallstep列表沒有被使用我在調試時甚至沒有使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.