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

fix(Contracts.EFCore): Fix association delete bug when there is a value object in the navigation property #250

Merged
merged 2 commits into from
Sep 20, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public void OnExecuting(ChangeTracker changeTracker)
var entries = changeTracker.Entries().Where(entry => entry.State == EntityState.Deleted && entry.Entity is ISoftDelete);
foreach (var entity in entries)
{
HandleNavigationEntry(entity.Navigations.Where(n => (n.Metadata is not ISkipNavigation) && !((IReadOnlyNavigation)n.Metadata).IsOnDependent));
var navigationEntries = entity.Navigations
.Where(navigationEntry => navigationEntry.Metadata is not ISkipNavigation &&
!((IReadOnlyNavigation)navigationEntry.Metadata).IsOnDependent && navigationEntry.CurrentValue != null &&
entries.All(e => e.Entity != navigationEntry.CurrentValue));
HandleNavigationEntry(navigationEntries);

entity.State = EntityState.Modified;
entity.CurrentValues[nameof(ISoftDelete.IsDeleted)] = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests;

public class CustomDbContext : MasaDbContext<CustomDbContext>
{
public CustomDbContext(MasaDbContextOptions<CustomDbContext> options) : base(options)
{
}

protected override void OnModelCreatingExecuting(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new StudentEntityTypeConfiguration());
modelBuilder.ApplyConfiguration(new AddressEntityTypeConfiguration());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests;
Expand Down Expand Up @@ -44,4 +44,57 @@ public void TestDataFilterReturnFalse()

Assert.IsFalse(_dataFilter.IsEnabled<ISoftDelete>());
}

[TestMethod]
public void TestSoftDelete()
{
var services = new ServiceCollection();
services.AddMasaDbContext<CustomDbContext>(options =>
{
options.UseTestSqlite($"data source=disabled-soft-delete-db-{Guid.NewGuid()}").UseFilter();
});
var serviceProvider = services.BuildServiceProvider();
var dbContext = serviceProvider.GetRequiredService<CustomDbContext>();
dbContext.Database.EnsureCreated();

DateTime createTime = DateTime.Now;
var student = new Student()
{
Id = 1,
Name = "Name",
Age = 18,
Address = new Address()
{
City = "city",
Street = "street",
LastLog = new LogItem()
{
Level = (int)LogLevel.Information,
Message = "Add Student",
CreateTime = createTime
}
},
Hobbies = new List<Hobby>()
{
new()
{
Name = "Hobby.Name",
Description = "Hobby.Description"
}
}
};
dbContext.Set<Student>().Add(student);
dbContext.SaveChanges();

student = dbContext.Set<Student>().Include(s => s.Address).FirstOrDefault(s => s.Id == 1);
Assert.IsNotNull(student);
dbContext.Set<Student>().Remove(student);
var row = dbContext.SaveChanges();
Assert.IsTrue(row > 0);

var newStudent = dbContext.Set<Student>().IgnoreQueryFilters().FirstOrDefault(s => s.Id == student.Id);
Assert.IsNotNull(newStudent);

Assert.AreEqual(createTime, newStudent.Address.LastLog.CreateTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.EntityConfigurations;

public class AddressEntityTypeConfiguration : IEntityTypeConfiguration<Address>
{
public void Configure(EntityTypeBuilder<Address> builder)
{
builder.OwnsOne(address => address.LastLog, address =>
{
address.Property(log => log.Level).HasColumnName("level").IsRequired();
address.Property(log => log.Message).HasColumnName("message").IsRequired();
address.Property(log => log.CreateTime).HasColumnName("create_time").IsRequired();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.EntityConfigurations;

public class StudentEntityTypeConfiguration : IEntityTypeConfiguration<Student>
{
public void Configure(EntityTypeBuilder<Student> builder)
{
builder.HasOne(s => s.Address).WithOne(a => a.Student).HasForeignKey<Address>(t => t.Id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Orm\EFCore\Masa.Contrib.Data.EFCore.Sqlite\Masa.Contrib.Data.EFCore.Sqlite.csproj" />
<ProjectReference Include="..\..\Masa.Contrib.Data.Contracts.EFCore\Masa.Contrib.Data.Contracts.EFCore.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;

public class Address : ISoftDelete
{
public int Id { get; set; }

public string City { get; set; } = default!;

public string Street { get; set; } = default!;

public LogItem LastLog { get; set; }

public Student Student { get; set; }

public bool IsDeleted { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;

public class Hobby
{
public Guid Id { get; set; }

public string Name { get; set; } = default!;

public string Description { get; set; } = default!;

public Hobby()
{
Id = Guid.NewGuid();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;

public class LogItem
{
public int Level { get; set; }

public string Message { get; set; }

public DateTime CreateTime { get; set; }

public LogItem()
{
CreateTime = DateTime.Now;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;

public class Student : ISoftDelete
{
public int Id { get; set; }

public string Name { get; set; } = default!;

public int Age { get; set; }

public bool IsDeleted { get; private set; } = default!;

public Address Address { get; set; } = default!;

public List<Hobby> Hobbies { get; set; } = default!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@

global using Masa.BuildingBlocks.Data.Contracts;
global using Masa.Contrib.Data.Contracts.EFCore.DataFiltering;
global using Masa.Contrib.Data.Contracts.EFCore.Tests.EntityConfigurations;
global using Masa.Contrib.Data.Contracts.EFCore.Tests.Models;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.EntityFrameworkCore.Metadata.Builders;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
global using Microsoft.VisualStudio.TestTools.UnitTesting;
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ protected ServiceBase(IServiceCollection services)
[Obsolete("service can be ignored")]
protected ServiceBase(IServiceCollection services, string baseUri) : this(services)
{

}
#pragma warning restore S4136

Expand Down