Author |
Share Topic Topic Search Topic Options
|
Amy
Newbie
Joined: 09-Dec-2008
Posts: 9
|
Post Options
Quote Reply
Topic: Foreign Key Questions Posted: 01-Jun-2009 at 9:45am |
Our database has a lot FKs that are integers. Some of them are required filed, some of them are not. I have some questions:
1. For required FK, how do you write your own verifier? DevForce’s default verifier for required FK is always returns OK, no matter what. I tried to write a range verifier for the required FK, it always returns OK too. How do you enforce the required FK?
2. In edit screen, how do you fill the ComboBox of non-required FK? Add a null entity? Since it is not required, the user should be able to change the ComboBox to select nothing.
Any help/suggestion would be appreciated.
Thanks!
Amy
|
|
davidklitzke
IdeaBlade
Joined: 14-Jun-2007
Posts: 715
|
Post Options
Quote Reply
Posted: 02-Jun-2009 at 8:13am |
For answers and tips on writing your own verifiers, see the Intermediate tutorial on "Verification".
For an example of the use of a combobox that uses the Null Entity, see the Intermediate tutorial on "Working with ComboBoxes". In this tutorial, a Null Entity is used to indicate that an Employee has no Manager.
|
|
Amy
Newbie
Joined: 09-Dec-2008
Posts: 9
|
Post Options
Quote Reply
Posted: 02-Jun-2009 at 11:19am |
#1 I mentioned in my question: I did add a range verifier, but it returns OK. Is anything wrong with following code?
private static Verifier GetOperationIDRequiredVerifier()
{ return new Int32RangeVerifier(typeof(Vehicle), Vehicle.Operation_fk_OperationIDEntityProperty.Name,
true, 0, int.MaxValue);}
#2 We also added an NullEntity on each non-requied FK. Since we have a lot FKs, wish there is a better solution.
Thanks!
|
|
eileenv
IdeaBlade
Joined: 15-Jun-2007
Location: United States
Posts: 68
|
Post Options
Quote Reply
Posted: 18-Jun-2009 at 2:39pm |
What is your rule for "Required FKs"? Is it that the value must be greater than 0? If the FKs are not nullable ints, then you would need some numeric rule for determining whether or not the FK is valid.
Here is how to write a verifier that says "ID must be greater than 0":
private static Verifier GetOperationIDRequiredVerifier() {
return new Int32RangeVerifier(
typeof(Vehicle), Vehicle.Operation_fk_OperationIDEntityProperty.Name,
true,
0, // minValue = 0
false, // do not include the minEndpoint
null, // don't care about maxValue
false); // do not include the maxEndpoint
}
The problem with the original verifier you wrote is that you were basically saying that any ID from 0 to the maximum integer value is OK and valid.
|
|
jeffdoolittle
Senior Member
Joined: 14-Jun-2007
Location: United States
Posts: 146
|
Post Options
Quote Reply
Posted: 18-Jun-2009 at 8:50pm |
I haven't done this for EF yet, but in DevForce classic, I took a different approach that worked really well for discovering potential FK violations prior to saving to the database.
1) I created an algorithm that looks at all EntityRelations and discovers all relations where the parent entity is required. This process discovers the parent property descriptor for the parent relation and looks for the "RequiredValueVerifierAttribute" on that property. It's a bit complex, but it works great for discovering the parent required properties.
2) I created a "NotNullEntityVerifier" that fails if an entity is the null entity.
3) I created a helper method that, when given an entity type, adds a NotNullEntityVerifier to the VerifierEngine for all required parent properties of the given type.
When all is said and done, all I have to do is go to my GetVerifiers static method for an entity type and do this:
[VerifierProvider] public static IEnumerable<Verifier> GetVerifiers(Object pVerifierProviderContext) { var engine = GetEngineFromVerifierProviderContext(pVerifierProviderContext); var list = new List<Verifier>(); list.AddParentRequiredVerifiers<Employee>(); return list; }
If you are so inclined, you could even call this method for all entity types in your Model assembly all at once rather than calling this once for each entity type (though in a large model this may not be advisable).
It's almost like magic when your Foreign Keys are enforced completely in the middle tier.
If any one is interested in the code, post on this thread and I'll try to find time to clean up and add this code to the DevForceContrib project on CodePlex (http://devforcecontrib.codeplex.com/).
--Jeff
|
|
Amy
Newbie
Joined: 09-Dec-2008
Posts: 9
|
Post Options
Quote Reply
Posted: 24-Jun-2009 at 9:28am |
Hi Jeff,
I am interested in the code. Please post it whenever you have time.
Thank you very much!
Amy
|
|
Amy
Newbie
Joined: 09-Dec-2008
Posts: 9
|
Post Options
Quote Reply
Posted: 28-Jun-2009 at 7:20am |
Check if FK > 0 is not really working. When adding parent, also adding related children in the same time, the FKs for children are negative.
Amy
|
|