-
How can I define a ManyToMany / OneToMany association with Linq2Db, preferably using a intermediate (join) table? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
We support only OneToOne/OneToMany associations. You can check various examples here https://github.com/linq2db/linq2db/blob/master/Tests/Linq/Linq/AssociationTests.cs Cardinality defined by type of association: Join condition could be defined as set of key fields, custom sql or even custom sub-query (see tests for examples). For |
Beta Was this translation helpful? Give feedback.
-
Does that have to be an association like class A
{
[Association(...)]
List<IntermediateTableB> Bs { get; set; }
} Or is there a way, maybe using static methods, to achieve a 'normal' mapping? Like: class A
{
[Association(...)]
List<C> Cs { get; set; }
} |
Beta Was this translation helpful? Give feedback.
-
That looks a bit more complex than I had anticipated 😄 |
Beta Was this translation helpful? Give feedback.
-
For future references and for others: Thanks to the examples from @MaceWindu, I manged to implement the following solution the retrieve my ManyToMany entries: [Association(QueryExpressionMethod = nameof(ACExpression))]
public ICollection<C> Cs { get; set; }
public static Expression<Func<A, IDataContext, IQueryable<C>>> ACExpression =>
(a, db) => db.GetTable<C>()
.InnerJoin(
db.GetTable<B>(),
(c, b) =>
c.Id == b.CId &&
a.Id == b.AId,
(c, b) => c); |
Beta Was this translation helpful? Give feedback.
We support only OneToOne/OneToMany associations. You can check various examples here https://github.com/linq2db/linq2db/blob/master/Tests/Linq/Linq/AssociationTests.cs
Cardinality defined by type of association:
TRecord
orIEnumerable<TRecord>
. You can also use other collection types, likeIQueryable<T>
, array,List<T>
, but I personally preferIEnumerable<T>
as it doesn't insinuate additional functionality, not really supported here.Join condition could be defined as set of key fields, custom sql or even custom sub-query (see tests for examples).
For
ManyToMany
association you will need to define intermediate table mapping wtih associations to both sides.