使用Fluent Api的零核或一对一关系EF核心

时间:2017-10-04 11:32:40

标签: c# entity-framework orm ef-code-first ef-core-2.0

如果我有模特:

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);
    }
}

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您没有删除帐户实体。您只是在与用户建立关系。此外,您在Fluent API中配置的是&#34;如果删除了用户实体,则删除相关帐户实体。&#34;而不是这个。

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();
}

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