Print Page | Close Window

Property Uniqueness?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3617
Printed Date: 13-May-2026 at 1:03am


Topic: Property Uniqueness?
Posted By: jradxl
Subject: Property Uniqueness?
Date Posted: 10-Sep-2012 at 8:53am
Hi,
If a property needs to be unique (such as UserName or EmailAddress) but not a primary key how does one go about adding it in Code First?
Is there an Attribute?
OK, the UI can inforce it, but is there a Devforce database compatible way?
thanks
John




Replies:
Posted By: kimj
Date Posted: 10-Sep-2012 at 10:52am
DevForce is relying on the EF Code First attributes (or Fluent API) to define the model, and right now EF does not support unique keys.  There are a couple of options though.
 
To add a unique constraint or index to the database table you can write a custom DbInitializer and create the index within the Seed method logic, or as of EF 4.3 use Db migrations.   Here's some more information from StackOverflow - http://stackoverflow.com/questions/4413084/unique-constraint-in-entity-framework-code-first - http://stackoverflow.com/questions/4413084/unique-constraint-in-entity-framework-code-first  and http://stackoverflow.com/questions/8262590/entity-framework-code-first-fluent-api-adding-indexes-to-columns - http://stackoverflow.com/questions/8262590/entity-framework-code-first-fluent-api-adding-indexes-to-columns .
 
You can also write a custom DevForce verifier to validate the property when it's set or the entity is saved - http://drc.ideablade.com/xwiki/bin/view/Documentation/validation-create-custom-verifier - http://drc.ideablade.com/xwiki/bin/view/Documentation/validation-create-custom-verifier .


Posted By: mgood
Date Posted: 10-Sep-2012 at 1:15pm
John,
Checking uniqueness takes a little bit more thought than simply putting a uniquness constraint on the property, unless you are perfectly happy with an exception if you are trying to save a duplicate. If that's the case, then stop reading. If you however want to alert the user before it hits the database, then read on.
 
Checking if a property is unique requires querying the DB. In Silverlight that is an asynchronous operation. In WPF it should be an asynchronous operation if you want a good performing application. The proper approach is a validation service that the ViewModel can asynchronously call before saving the data. There's still the possiblity that you get a constraint violation in the DB if an other user snuck in a save between you checking for uniquness and saving the data, so you should still have a DB constraint.
 
You can find more information and an example here:
 
http://drc.ideablade.com/xwiki/bin/view/Documentation/unit-of-work-pattern-and-persistence-ignorance#HDomainService - http://drc.ideablade.com/xwiki/bin/view/Documentation/unit-of-work-pattern-and-persistence-ignorance#HDomainService


Posted By: jradxl
Date Posted: 10-Sep-2012 at 2:14pm
Thank you both for your quick and full responses.
Informing the User and a responsive application is indeed my goal, and so this is very useful. Indeed async operation in a WPF app was not something I had immediately thought of.
Thanks
John



Print Page | Close Window