Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle adding optional dependents using table splitting without principal. #25757

Merged
merged 1 commit into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Update/ModificationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private List<IColumnModification> GenerateColumnModifications()
|| (!isKey && nonMainEntry))
{
writeValue = columnPropagator?.TryPropagate(property, entry)
?? entry.IsModified(property);
?? (entry.EntityState == EntityState.Added || entry.IsModified(property));
}
}

Expand Down
110 changes: 110 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/SqlServerEndToEndTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -298,6 +299,115 @@ public ENumContext(DbContextOptions options)
public DbSet<BNum> BNums { get; set; }
}

[ConditionalFact]
public void Can_add_table_splitting_dependent_after_principal()
{
using var testDatabase = SqlServerTestStore.CreateInitialized(DatabaseName);

var options = Fixture.CreateOptions(testDatabase);
EvaluationAction evaluationAction = null;
using (var context = new ProjectContext(options))
{
context.Database.EnsureCreatedResiliently();

evaluationAction = new EvaluationAction()
{
Id = Guid.NewGuid().ToString(),
CreateId = "1",
UpdateId = "1"
};
context.EvaluationActions.Add(evaluationAction);
context.SaveChanges();
}

using (var context = new ProjectContext(options))
{
context.Database.EnsureCreatedResiliently();

var projectAction = new ProjectAction()
{
Id = evaluationAction.Id,
CreateId = "1",
UpdateId = "1",
Name = "123123123123"
};
context.ProjectActions.Add(projectAction);
context.SaveChanges();
}

using (var context = new ProjectContext(options))
{
Assert.NotNull(context.ProjectActions.Single());
Assert.NotNull(context.EvaluationActions.Single());
}
}

[ConditionalFact]
public void Throws_when_adding_table_splitting_dependent_without_principal()
{
using var testDatabase = SqlServerTestStore.CreateInitialized(DatabaseName);

var options = Fixture.CreateOptions(testDatabase);
using (var context = new ProjectContext(options))
{
context.Database.EnsureCreatedResiliently();

var projectAction = new ProjectAction()
{
Id = Guid.NewGuid().ToString(),
CreateId = "1",
UpdateId = "1",
Name = "123123123123"
};
context.ProjectActions.Add(projectAction);

Assert.Throws<DbUpdateConcurrencyException>(() => context.SaveChanges());
}
}

private class ProjectContext : DbContext
{
public ProjectContext(DbContextOptions options)
: base(options)
{
}

public DbSet<EvaluationAction> EvaluationActions { get; set; }
public DbSet<ProjectAction> ProjectActions { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ProjectAction>()
.ToTable("projectaction")
.HasOne(o => o.EvaluationAction).WithOne(o => o.ProjectAction)
.HasForeignKey<ProjectAction>(o => o.Id);

modelBuilder.Entity<ProjectAction>().Property(p => p.Name).IsRequired();

modelBuilder.Entity<EvaluationAction>()
.ToTable("projectaction");
}
}

private class ProjectAction
{
public string Id { get; set; }
public string CreateId { get; set; }
public string UpdateId { get; set; }
public string Name { get; set; }

public EvaluationAction EvaluationAction { get; set; }
}

private class EvaluationAction
{
public string Id { get; set; }
public string CreateId { get; set; }
public string UpdateId { get; set; }

public ProjectAction ProjectAction { get; set; }
}

private class SNum
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
Expand Down