Suppose I have a DB as shown. For now I want to focus on the relationships among the tables.
DB structure
************
table1: a
(int), b
(string), c
(string)
table2: a
(int), d
(int), e
(string)
{table2.a references table1.a}
table3: d
(int), f
(string), g (string)
{table3.d references table2.d}
table4: a
(int), h
(string), i
(string)
{table4.a references table1.a}
I found that these relationships could be replicated and enforced at an object level also. I think I found the below idea from MSDN, though I am not sure.... Anyways, according to that concept, I have the below code for the objects.
class table1
{
public Int32 a { get; set; }
public String b { get; set; }
public String c { get; set; }
public ICollection<table2> t2 { get; set; }
public ICollection<table4> t4 { get; set; }
public table1()
{
t2 = new List<table2>;
t4 = new List<table4>;
}
}
class table2
{
public Int32 a { get; set; }
public Int32 d { get; set; }
public String e { get; set; }
public ICollection<table3> { get; set; }
public virtual table1 { get; set; }
public table2()
{
t3 = new List<table3>;
}
}
class table3
{
public Int32 d { get; set; }
public String f { get; set; }
public String g { get; set; }
public virtual table2 { get; set; }
}
class table4
{
public Int32 a { get; set; }
public String h { get; set; }
public String i { get; set; }
public virtual table1 { get; set; }
}
Now, I can see the advantage in this. I mean, I can get the parent table table1's object and along with it I also get a list of related data from table2 and table4 in the same object and so on...
I can assign the related data to a new table2 object, and add that object to table1 object's ICollection<table2> field thing.
What I am not sure of is, the 'virtual' member of objects table2, table3 and table4... I can understand that they refer to the respective 'parent' Object... But how do I assign the parent table's related values to this member? I mean, being virtual, VS throws an error if I try to assign values to them in the DAL layer... Same thing happens when i hover the cursor over the object and when the tooltip/hint thing that shows the object's content pops up, i can see the values of all the members of the object, except for 'virtual' members if any. The virtual members show an error of not being able to be evaluated or so and the program seems to exit or crash...
So, how do i exactly use the virtual members?