EF-一对一关系 [英] EF - One to one relationship

查看:99
本文介绍了EF-一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

public class FinanceiroLancamento
{
    /// <summary>Identificação</summary>
    public override int Id { get; set; }

    /// <summary>Financeiro caixa</summary>
    public FinanceiroLancamentoCaixa FinanceiroLancamentoCaixa { get; set; }
}

public class FinanceiroLancamentoCaixa
{
    /// <summary>Identificação</summary>
    public override int Id { get; set; }

    /// <summary>Identificação do lançamento financeiro</summary>        
    public int IdFinanceiroLancamento { get; set; }
}

当我尝试映射并执行迁移时,它会返回:

When I try to map and execute migration it´s return:

属性名称'IdFinanceiroLancamento'已经定义.

Property name 'IdFinanceiroLancamento' was already defined.

要解决此问题,我需要评论idfinanceirolancamento并按以下方式映射:

To solve this problem I needed to comment idfinanceirolancamento and map like this:

        HasRequired(e => e.FinanceiroLancamentoCaixa)
            .WithRequiredPrincipal()
            .Map(m => m.MapKey("IdFinanceiroLancamento"));

问题是:我该如何在FK(FinanceiroLancamento-> FinanceiroLancamentoCaixa)中保留"IdFinanceiroLancamento {get; set;}"?

The question is: How can I this FK (FinanceiroLancamento -> FinanceiroLancamentoCaixa) keeping the "IdFinanceiroLancamento { get; set; }"?

就我而言,这对于在课程的后面使用非常重要.

This is very important in my case to use later in the class.

Ps:FinanceiroLancamento不需要FinanceiroLancamentoCaixa,但是当存在FinanceiroLancamentoCaixa时,他需要FinanceiroLancamento.

Ps: FinanceiroLancamento does not need a FinanceiroLancamentoCaixa, but when FinanceiroLancamentoCaixa exists he needs a FinanceiroLancamento.

最诚挚的问候.

Wilton Ruffato Wonrath

Wilton Ruffato Wonrath

推荐答案

实体框架要求1:1映射共享相同的主键.在您的情况下,您尝试使用其他成员作为映射ID.另外,请勿覆盖基类ID,而要继承它.

Entity Framework requires that 1:1 mappings share the same primary key. In your case, you are trying to use a different member as the mapping id. Also, do not override the base class id, just inherit it.

您想要什么?

.HasRequired(e => e.FinanceiroLancamentoCaixa)
.WithRequiredPrincipal();

实体框架不允许您使用不是共享主键的1:1,因此您不能在EF中使用它.如果您绝对需要此功能,则可能必须将其作为存储过程并从EF调用.

Entity Framework does not allow you to use a 1:1 that is not a shared primary key, so you can't do it in EF. If you absolutely need this, you may have to do it as a stored procedure and call it from EF.

之所以不能像这样设置1:1,是因为数据模型允许您在多个记录中将IdFinanceiroLancamento设置为相同的ID,从而破坏了1:1.

The reason you can't have a 1:1 like this is because the data model allows you to set IdFinanceiroLancamento to the same ID in more than one record, thus breaking your 1:1.

基本上,EF不允许您使用带有违反映射关系的映射关系来创建模型,即使您从未创建重复项,也仍然有可能.EF也不知道唯一约束,因此放置唯一约束不会告诉EF没关系.

Basically, EF will not allow you to create models with mappings that allow for a violation of the mapping, even if you never create duplicates, it's still a possibility. EF doesn't know about unique constraints either so placing a unique constraint won't tell EF that it's ok.

如果您想查看此功能,建议您在EF用户语音中对其进行投票:

If you'd like to see this feature, I suggest you vote for it at the EF uservoice:

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆