Hello,
***
(Its a very simple question...draw it on paper if you don't understand it)
***
And with this type of comments you definitely won't get answers... ;-)
***
The other part
***
Yes, the ForeignKeyConstraint does take references to the DataColumns and
the Table and store them internally, but enforcing the constraint does not
start from the constraint but from the update, insert, delete etc operation
. So if you don't add the constraint to the Constraints collection (of
either table) it is now known when constrains are checked. i.e the
constraint has correct references but the operation that uses the constraint
to check stuff isn't aware of constraint's existence.
Take an example. This code happens when row is changing in DataTable:
**
enumerator1 = this.Constraints.GetEnumerator();
try
{
while (enumerator1.MoveNext())
{
constraint1 = ((Constraint) enumerator1.Current);
constraint1.CheckConstraint(row1, action1);
}
}
**
You clearly see that Constraints collection is referenced to get to check
the constrains. If constraint does not exist there, it is hard to ensure it.
Ok then another example, consider the code:
***
//Create the DataSet
DataSet ds=new DataSet();
//First table
DataTable dt1=new DataTable();
ds.Tables.Add(dt1);
dt1.Columns.Add("ID",typeof(int));
dt1.Columns.Add("text",typeof(string));
//Second table
DataTable dt2=new DataTable();
ds.Tables.Add(dt2);
dt2.Columns.Add("ID",typeof(int));
dt2.Columns.Add("ForeignID",typeof(int));
ForeignKeyConstraint cons=new
ForeignKeyConstraint("const",dt1.Columns["ID"],dt2.Columns["ForeignID"]);
//Add constraint to the table
dt2.Constraints.Add(cons) ;
//Create parent row
DataRow row=dt1.NewRow();
row[0]=1;
row[1]="something";
dt1.Rows.Add(row);
//Try to create child row with false data
row=dt2.NewRow();
row[0]=1;
//This breaks with the constraint
row[1]=2;
dt2.Rows.Add(row);
***
If you run it as is, you'll get exception:
"ForeignKeyConstraint const requires the child key values (2) to exist in
the parent table" because value 2 I try to add to the second table as
foreign key does not exists in the parent table. This is clear, OK.
1. Now change the code so that comment the line
//Add constraint to the table
//dt2.Constraints.Add(cons) ;
i.e constraint is not anymore added to any table
--> No exception is raised, means constraint is not ensured because it
doesn't exist in Constrains collection, even though it does reference the
parent/child columns but internally. This goes to the first set of code,
constraint is not in Constrains collection.
2. Change the same code but add the constraint to Table dt1:
//Add constraint to the table
dt1.Constraints.Add(cons) ;
Run the code--> You'll get exception: "This constraint cannot be added since
ForeignKey doesn't belong to table Table1". This means ForeignKeyConstraint
can only be added to the child table.
The internal reason for this is ForeignKeyConstraints internal method: which
is called when ForeignKeyConstraint is added to the collection.
***
internal override void CheckCanAddToCollection(ConstraintCollection
constraints)
{ if (base.Table != constraints.Table)
{
throw ExceptionBuilder.ConstraintAddFailed(constraints.Table);
}
}
***
Basically it means that ForeignKeyConstraint must be added to the child
table (as was clear after checking the previous code) as I said before4.
That check does it clearly that the Table the constraint references must be
same as the Constraints collection it is added to. More clearly the Table
property of the ForeignKeyConstraint returns the table like this:
return this.childKey.Table;
Which even more clearly indicates that constraint must be in child key's
table.
Is there anything more I can do for you?
--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
greg r said:
Actually, I have read all those documents..and they don't answer my
basic question about foreign keys!
In fact, they contradict several pieces of example code in the MSDN.
(I was expecting this snap answer)
Here is the question again:
- Suppose a DataRelation is NOT being used anywhere (forget the
DataRelation!)
- Suppose a ForeignKeyConstraint is created on parent table A and
child table B
Question: Does it matter which table (A or B) the ForeignKeyConstraint
is
added to?
(Its a very simple question...draw it on paper if you don't understand
it)
Ive seen samples where sometimes they add it to A, and sometimes
B...WITHOUT ANY explanation.
I don't see any reason its added it ANY table because it has both
tables and their columns stored inside the ForeignKey!
Its funny how people don't really explain this properly.
"Teemu Keiski" <
[email protected]> wrote in message
http://msdn.microsoft.com/library/d...l/cpconaddingrelationshipbetweentwotables.asp UniqueConstraint
http://msdn.microsoft.com/library/d...uide/html/cpconaddingconstraintstodataset.asp