实体框架非典型1- [0..1] - 仅模型关联 - EF LINQ选择可能性 [英] Entity Framework atypical 1-[0..1] -- model-only Association -- EF LINQ select possibilities

查看:107
本文介绍了实体框架非典型1- [0..1] - 仅模型关联 - EF LINQ选择可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下表格

  ParentEntities 
ParentID

ChildEntities
ChildID
ParentID

这些表在模式中没有定义FK。 >

在EF设计器中,从DB生成后,我添加一个关联:


- 父多重:1


- 孩子多重性:0或1



当我构建时,我收到错误:错误3027:没有为以下EntitySet / AssociationSet指定映射 - ParentChild



但是,如果我尝试为这样的关联配置表格映射。

 映射到ChildEntities 

ParentID< - > ParentID(父实体支持< - >子表列)

子项
ChildID< - > ChildID(子实体支持< - >子表列)

..我得到: em>错误3007:以xxx,xxx行开始映射片段的问题:列([ParentID])将两个片段映射到不同的概念侧属性。



为什么这是一个错误没有意义。限制目前的实施情况?







我可以通过创建1-N关联来使其工作。这不是理想的,但它的工作原理相同,只需要在一个部分添加只读子属性:

  public父类
{
public Child Child {get {return Childs.Any()? null:Childs.First(); }}
}

这似乎是我最好的解决方案。我不得不向数据库添加一个FK以获得EF来生成关联和导航属性,但一旦添加,我就可以删除FK,并且从DB中进一步更新模型并没有删除关联或导航属性。






正在调查如何解决不关心正在建模的关联在EF中,我遇到了另一个问题。而不是只读的Child属性,我使它正常..

  public partial class Parent 
{
public Child Child {get;组; }
}

..但现在我需要一种从查询中实现的方法:

  var query =来自上下文中的父母.Parents 
//无意义加入给定选择
加入小孩context.Childs
在parents.ParentID等于child.ParentID
选择父母;



我可以选择匿名类型..

  //步骤1 
var query =来自上下文中的父母.Parents
在上下文中连接子项
parents.ParentID equals child.ParentID
select new {Parent = parents,Child = child};

..但是我必须消耗更多的周期才能进入我的实体:

  //步骤2 
var results = query.Select(x => {
var parent = x。 Parent;
parent.Child = x.Child;
return parent;});

是否有更好/简化的方式从查询选择中执行此操作,以便EF物理器可以执行此操作从逍遥游如果没有,那么我将使用Edit 1方法。

解决方案

Ef代码首先需要1-> 0..1孩子的关系具有相同的主键。
也许这是一个类似的限制在这种情况下的建模者。
两个表中都需要ParentId(Key)。



我从来没有尝试过在DB中的设计师后缀中添加这样的关系。



编辑:匹配你的EDIT2:



我会坚持的方向。使用导航属性从加入回到原始的类A和B。

  query = context.Set< JoinTable> .Where Jt => Jt.NavA.Id == ClassAId 
&& Jt.navB.Id == ClassBId)

如果您需要从ClassA或ClassB返回的条目,请使用select。


Suppose the following tables

 ParentEntities
    ParentID

 ChildEntities
    ChildID
    ParentID

These tables do not have a FK defined in the schema.

In EF designer, after generating from DB, I add an association:
- Parent Multiplicity: 1
- Child Multiplicity: 0 or 1

When I build, I get the error: "Error 3027: No mapping specified for the following EntitySet/AssociationSet - ParentChild"

But if I try to configure table mapping for the association like this..

Maps to ChildEntities 
  Parent
    ParentID <-> ParentID (parent entity prop <-> child table column)

  Child
    ChildID <-> ChildID (child entity prop <-> child table column)

.. I get this: Error 3007: Problem in mapping fragments starting at lines xxx, xxx: Column(s) [ParentID] are being mapped in both fragments to different conceptual side properties.

Why this is an error doesn't make sense. Limitation of the current implementation?


[EDIT 1]
I'm able to make this work by creating a 1-N association. That's not ideal, but it works just the same, just have to add a read-only child property in a partial:

public partial class Parent
{
    public Child Child { get { return Childs.Any() ? null : Childs.First(); } }
}

This seems like the best solution for me. I had to add a FK to the database to get EF to generate the association and navigation property, but once it was added I was able to remove the FK, and further updates to the model from the DB did not remove the association or Navigation properties.


[EDIT 2]
As I was investigating how to work around not caring about the association being modeled in EF, I ran into another issue. Instead of the read-only Child property I made it normal ..

public partial class Parent
{
    public Child Child { get; set; }
}

.. but now I need a way to materialize that from the query:

var query = from parents in context.Parents
            // pointless join given select
            join child in context.Childs 
                on parents.ParentID equals child.ParentID
            select parents;


I can select an anonymous type ..

// step 1
var query = from parents in context.Parents
            join child in context.Childs
                on parents.ParentID equals child.ParentID
            select new { Parent = parents, Child = child };

.. but then I've got to consume more cycles getting that into my entity:

 // step 2
 var results = query.Select(x => {
                   var parent = x.Parent;
                   parent.Child = x.Child; 
                   return parent; });

Is there a better/streamlined way to do this from the query select so the EF materializer can do it from the get-go? If not, then I'll resort to Edit 1 methodology ..

解决方案

Ef Code first requires 1->0..1 relationships for the Child to have the same primary key. Maybe this a similar restriction In the modeler in this circumstance. ParentId (Key) required in Both tables.

I have never tried adding such relationships in designer afterwords in DB first.

EDIT: to match your EDIT2:

I would stay on the direction . Use Navigation properties to get from Join back to original class A and B.

query = context.Set<JoinTable>.Where(Jt=>Jt.NavA.Id == ClassAId
                                  && Jt.navB.Id == ClassBId)

use a select if your need entries returned from either ClassA or ClassB.

这篇关于实体框架非典型1- [0..1] - 仅模型关联 - EF LINQ选择可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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