Christie
Christie

Reputation: 113

How to structure entity classes with the following relationship in SQL

I have 3 tables with the following relationships in an SQLite database;

A has many B has many C

What is the best way to structure these tables as entity classes in C#?

The two possible solutions I have come up with are:

1.)

class A{
    string id;
    List<B>;
}

class B{
    string id;
    List<C>;
}
class C{
    string id;
}

However I don't like this solution because of the nested looping required to access C from A for example.

2.)

class A{
    string id;
}
class B{
    string id;
    A();
}
class C{
    string id;
    B();
}

Again I'm not sure about this solution as it may involve writing complicated methods for accessing data across different lists.

So I'm asking, is there a better, cleaner solution for this problem?

Many thanks in advance.

Upvotes: 0

Views: 79

Answers (2)

BrianM
BrianM

Reputation: 866

You do both:

public class A
{
    public string id { get; set; }
    public ICollection<B> bs { get; set; }
}

public class B
{
    public string id { get; set; }
    public ICollection<C> cs { get; set; }
    public A a { get; set; }
}

public class C
{
    public string id { get; set; }
    public B b { get; set; }
}

Upvotes: 1

UMKalaycı
UMKalaycı

Reputation: 83

you can be arranged this way.

Class A{
int a;
List<B> bList;
}
Class B{
int b;
List<C> cList;
A a;
}
Class C{
int c;
B b;
}

Upvotes: 0

Related Questions