I'm trying to create a foreign key in a class, B, to the id of another class, A. The problem is that I can't verify the ID of A's objects until near the end of the program when all my instances of B have already been created. I would like to be able to instantiate objects of B with a reference to the id of an A object so that when the id of the A object changes, so does the value of the foreign key in B. I know I can't do that directly in C# without using 'unsafe' mode so I've been looking for alternative methods. As a last resort I could do a loop at the end through all my B objects, but B is actually many different classes and objects and there are enough of them that I would much prefer a more efficient solution.
Here's a rough example of the logic that I need.
using System;
namespace sandbox
{
public class A
{
public int ID;
public A(int id)
{
ID = id;
}
}
public class B
{
public int reftoAID;
public B(int id)
{
reftoAID = id;
}
}
public class TestMain
{
static void Main()
{
A a = new A(1);
B b = new B(a.ID);
a.ID = 3;
Console.WriteLine(b.reftoAID); //prints 1, but I need 3
Console.ReadLine();
}
}
}
I have tried creating a wrapper class with an int property that holds the id, but I haven't been able to figure out how to get it into the database using linqtodb. I thought I would be able to just define an (int) cast, but that doesn't work. From what I can tell, linqtodb uses the System.Data.DbType enum to determine the type and I don't know if there's a way to have it convert before the lookup.
I'm looking either for a new approach to the problem or a way of converting the wrapper class in such a way that it can be written to the database.