实体框架一对二关系,查询中未使用外键,为什么?

问题描述

我们有两个应用程序使用同一个数据库一个应用程序是使用 ef 核心的 .NET 核心应用程序,另一个是使用 ef6 的 .net 4.6.1 MVC 应用程序。我们在 .net 核心应用程序中构建了一个规则引擎,现在正尝试将该引擎引入我们的 MVC 应用程序,但在处理 EF 关系时遇到了困难。

我们需要两个 one2one 表:

public class RuleSet
{
    public int Id { get; set; }

    public virtual ActionSet ActionSet { get; set; }
}

public class ActionSet
{
    public int Id { get; set; }

    public int RuleSetId { get; set; }

    public virtual RuleSet RuleSet { get; set; }
}

在我们流畅的模型构建器中,我已经尝试过

        modelBuilder.Entity<RuleSet>()
            .Hasrequired(e => e.ActionSet)
            .WithrequiredPrincipal(x => x.RuleSet)
            .WillCascadeOnDelete();

这导致两个项目都被主键“Id”拉入,依赖项不使用 RuleSetId 作为它的关联。从而引入了错误的动作集。

我们注意到在将 HasKey 从 Id 更改为 RuleSetId 时,我们拉入了正确的 ActionSet,但现在所有对 ActionSet 的依赖都不正确。

        // brings in the right ActionSet,but the wrong dependents for ActionSet
        modelBuilder.Entity<BossActionSet>()
            .HasKey(x => x.RuleSetId); 

我们不希望更改两个应用程序中的代码,有人能想出一种方法可以在不更改模型的情况下实现我们的目标吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)