Hi,

Just started testing DataObjects.Net on a project. Here's my issue. Let's assume I have 2 entities mapped in a many-to-many relationship like this:

[HierarchyRoot]

 public class EntityA : Entity
{
    [Field, Key]
    public Int32 Id { get; private set; }

    [Field]
    public String Name { get; private set; }

    [Field]
    public EntitySet<EntityB> EntityBs { get; private set; }
}

[HierarchyRoot]
public class EntityB : Entity
{
    [Field, Key]
    public Int32 Id { get; private set; }

    [Field]
    public String Name { get; private set; }

    [Field]
    public EntitySet<EntityA> EntityAs { get; private set; }
}

Which seem to work just fine. But in my database, the relationship table AxB has some extra data regarding the relationship (the display order of the relationship entry).

How can I "access" this order value field so I can generate an ordered query on top of the EntitySet Collection?

I know I could theoretically solve the issue by promoting the relationship itself to an Entity, but that would seem to be a really poor entity to build on the code.

Is there anyway to achieve this? Is this, overall, a bad practise and there is a more elegant solution?

Regards,

Pedro

asked Jun 04 '12 at 16:14

pgoes's gravatar image

pgoes
216610


2 Answers:

Hello Pedro,

Consider introducing an intermediate EntityAB that holds references to EntityA and EntityB along with some additional data.

Hope that helps

answered Jun 05 '12 at 05:58

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

edited Jun 05 '12 at 06:24

That's what I thought, was just wondering if there was any way in which I could avoid it, since it would be a really poor entity. So far, at least, I thought about hiding EntityAB as private inside EntityA, and having EntityA still expose an EntityB collection which handles the "magic" and makes this EntityA-EntityAB-EntityB bridge transparent.

(Jun 05 '12 at 07:55) pgoes pgoes's gravatar image

Hello Pedro.

You can customize LINQ translation:

[HierarchyRoot]
public class EntityB : Entity
{
  ...
  [Field]
  public EntitySet<EntityAB> EntityABs { get; private set; }

  public IQueryable<EntityA> EntityA{get{return EntiteABs.Select(a=>a.EntityA);}}
}
[CompilerContainer(typeof (Expression))]
public static class CustomLinqCompilerContainer
{
  [Compiler(typeof (EntityB), "EntityA", TargetKind.PropertyGet)]
public static Expression EntityA(
  Expression entityExpression)
  {
    Expression<Func<EntityB, IQueryable<EntityA>> ex =  entity => entity.EntityABs.Select(a=>a.EntityA);
    return ex.BindParameters(entityExpression);
  }
}

P.S. this code may be contains typos.

answered Aug 02 '12 at 04:50

proff's gravatar image

proff
75336

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×4

Asked: Jun 04 '12 at 16:14

Seen: 1,717 times

Last updated: Aug 02 '12 at 04:50

powered by OSQA