Creating a DataRelation

R

Roshawn

Hi,

Creating a DataRelation among two tables seems to be pretty simple
(according to ADO.NET Core Reference). However, I'm not sure of the best
time to create it. Is it better to create the DataRelation between the
tables after the tables have been filled, or is it better to create
ForeignKeyConstraints when creating the columns of each table?

Roshawn
 
M

Miha Markic [MVP C#]

Hi,

Roshawn said:
Hi,

Creating a DataRelation among two tables seems to be pretty simple
(according to ADO.NET Core Reference). However, I'm not sure of the best
time to create it. Is it better to create the DataRelation between the
tables after the tables have been filled, or is it better to create
ForeignKeyConstraints when creating the columns of each table?

IMO the difference is the time you'll get an exception if reference can't be
enforced.
Doesn't matter otherwise.
 
J

Jay B. Harlow [MVP - Outlook]

Roshawn,
As Miha suggests, it doesn't matter.

I normally create them based on the needs of my project. In one project I
create all the DataTables, then all the DataRelations, then I fill the
dataset. In another project I create the parent DataTables, then as I create
a child DataTable I create that child DataRelation.

Note I normally create the PrimaryKey constraints & the DataRelation
explicitly, allowing the framework create any ForeignKeyConstraints &
UniqueConstraints implicitly as a side effect of creating the
DataRelation...

For strongly typed data sets they will be created when you instantiate a new
strongly typed dataset.

Also you can use DataSet.EnforceConstraints to control if the constraint is
enforced while filling a dataset, thus controlling if an exception is thrown
or not.
(according to ADO.NET Core Reference).
I was going to recommend Sceppa's book, but it sounds like you already have
it ;-)

Hope this helps
Jay
 
W

William Ryan eMVP

Roshawn:

I definitely agree with Miha and Jay although there's one case that I'd
qualify my agreement a little. If you build the relations up front - before
any data is entered, then you'll be able to know for sure that you don't
have any non conforming data or that you won't raise an exception. So
ultimately it depends on your situation and when/how/if you chose to handle
your violating records.

I'm with you on Sceppa's book - it Rocks. But as a group, we all need to
talk Pablo and Angel into working with David and writing an update for
ADO.NET 2.0 or writing a new Book.

--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
J

Jay B. Harlow [MVP - Outlook]

William,
Yes the why is important, your reason is more why I create the relations
before I fill the dataset...

To a lesser extent it keeps the creation code in one place, separate from
the filling code.

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top