Integrity constraints

To avoid the problems you saw in the last Thinking Hat, database engines support the following kinds of constraints. Most of these are enforced at the database level (not by Entity Framework).

Image Unique Constraint

Most database engines require that primary keys be unique, but you might also need to specify this constraint for other fields, particularly if your candidate key is not the primary key.

Image Referential Integrity Constraint

When two tables are related, you must ensure that all the values in the child table are present in the parent table. The database engine will enforce this when you establish the relationship.

Image Update Constraint

If the primary key of a parent table changes, the foreign key that references it in child tables must be updated as well. This is enforced at the database level using an UPDATE CONSTRAINT.

Image Delete Constraint

You’ll need to decide the action to take if a row in the parent table is deleted. It might be reasonable to set the foreign key to Null, but you’ll often want the entire child row to be deleted. That is enforced at the database level using a DELETE CONSTRAINT.

Image Not Null Constraint

Primary keys, other candidate keys, and sometimes other fields as well cannot be left empty. This is enforced by both the database and Entity Framework with a NOT NULL CONSTRAINT.


Image Put On Your Thinking Hat...Then Take a Break

Of all the constraints in the list above, only one, the Not Null Constraint, can be enforced by Entity Framework. Can you figure out why?

Now, why don’t you take a break before we turn our attention to the OOP side of the Entity Framework equation?



Image Review

“The key, the whole key and nothing but the key” is a good way to remember what?

 

What basic problem is database normalization intended to solve?

 

Why can’t Entity Framework enforce all the various constraints required to ensure database integrity?

 

What is a “foreign key”? What does it do?

 

 



Image How’d You Do?

The reason that Entity Framework can only enforce the Not Null constraint is that the remaining constraints require access to all the data in a table, and there’s no way for Entity Framework to know whether the data it’s managing is all or just a portion of the data stored in the database.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset