Thursday, 8 August 2013

One-side association in Linq to Sql

One-side association in Linq to Sql

Is there any way to create one-side association in Linq to Sql?
I must clarify that I'm using DbLinq instead of MS System.Data.Linq.
For example there is table A and B. Entities from A reference to entity in
table B. So we have many-to-one relationship.
Some code:
CREATE TABLE A
(
a_idx serial NOT NULL,
...
ref_b int,
CONSTRAINT a_to_b FOREIGN KEY (ref_b)
REFERENCES B (b_idx)
)
CREATE TABLE B
(
b_idx serial NOT NULL,
...
)
I know one way to create such association:
public class A
{
[Column(IsPrimary=true, DbGenerated=true)]
public int a_idx;
...
[Column]
private int ref_b;
EntityRef<B> _b;
[Association(IsForeignKey = true, ThisKey = "ref_b", OtherKey= "b_idx",
Storage = "_b", Name = "a_to_b")]
public B B { get & set here }
}
class B
{
[Column(IsPrimary=true, DbGenerated=true)]
public int b_idx;
EntitySet<A> _a;
[Association(ThisKey = "b_idx", OtherKey= "ref_b", Storage = "_a",
Name = "a_to_b")]
public ICollection<A> A { get & set here }
}
But I do not need A property in class B. And I couldn't make these
property private because in this case Linq stops working.

No comments:

Post a Comment