1

如果我有模型:

public class User
{
    public Account Account { get; set; }
    public string SomeInfo { get; set; }
    public Guid Id { get; set; }
}

public class Account
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public class UserEntityTypeConfiguration : IEntityTypeConfiguration<User>
{
    public virtual void Configure(EntityTypeBuilder<User> builder)
    {
        builder.HasKey(x => x.Id);
        builder
            .Property(t => t.Id)
            .ValueGeneratedOnAdd();
    }
}

当我在下面运行测试时,它显示该帐户已从用户中删除,但未从数据库中删除

[Test]
public async Task GivenAUser_WhenISetTheAccountToNull_ThenTheAccountIsRemovedFromTheDatabase()
{
    Guid userId;
    using (DbContext dbContext = GetDbContext())
    {
        UserRepository userRepository = new UserRepository(dbContext);
        User newUser = new User
        {
            SomeInfo = "SomeInfo",
            Account = new Account
            {
                Name = "Name"
            }
        };
        await userRepository.SaveAsync(newUser);
        userId = newUser.Id;
    }

    using (DbContext dbContext = GetDbContext())
    {
        UserRepository userRepository = new UserRepository(dbContext);
        User user = await userRepository.GetAsync(userId);
        user.Account = null;
        await userRepository.SaveAsync(user);
    }

    User userDb;
    int userAccountsCount;
    using (DbContext dbContext = GetDbContext())
    {
        UserRepository userRepository = new UserRepository(dbContext);
        userAccountsCount = await dbContext.Set<UserAccount>().CountAsync();
        userDb = await userRepository.GetAsync(userId);
    }

    Assert.That(userDb.Account, Is.EqualTo(null)); // true
    Assert.That(userAccountsCount, Is.EqualTo(0)); // false
}

我试图创建一个 UserEntityTypeConfiguration 来在可选字段上设置级联删除,但我似乎无法让它适用于可选字段。

public class UserEntityTypeConfiguration : IEntityTypeConfiguration<User>
{
    public virtual void Configure(EntityTypeBuilder<User> builder)
    {
        builder.HasKey(x => x.Id);
        builder
            .Property(t => t.Id)
            .ValueGeneratedOnAdd();

        builder.HasOne(x => x.Account)
            .WithOne()
            .OnDelete(DeleteBehavior.Cascade);
    }
}

任何人都可以帮忙吗?

4

2 回答 2

0

我可以问你是否需要严格来说他们被建议反对的一对一关系,因为无论如何数据库只会将其视为一对多。数据库实际上并不知道什么是一对一。一对一在做什么?

于 2017-10-04T11:36:26.713 回答
0

您没有删除 Account 实体。您只是在破坏与用户的关系。此外,您在 Fluent API 中配置的是“如果用户实体被删除,则删除相关的帐户实体”。而不是这个。

using (DbContext dbContext = GetDbContext())
{
    UserRepository userRepository = new UserRepository(dbContext);
    User user = await userRepository.GetAsync(userId);
    user.Account = null;
    await userRepository.SaveAsync(user);
}

您应该删除实体本身。

using (DbContext dbContext = GetDbContext())
{
    UserRepository userRepository = new UserRepository(dbContext);
    User user = await userRepository.GetAsync(userId);
    Account acc = user.Account;
    dbContext.Entity(acc).State = EntityState.Deleted;
    dbContext.SaveChangesAsync();
}

如果您想使用存储库模式,您可以自由实现自己的解决方案。

于 2017-10-04T21:13:16.240 回答