New Posts New Posts RSS Feed: Property Uniqueness?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Property Uniqueness?

 Post Reply Post Reply
Author
jradxl View Drop Down
Newbie
Newbie


Joined: 14-Jun-2011
Posts: 30
Post Options Post Options   Quote jradxl Quote  Post ReplyReply Direct Link To This Post Topic: Property Uniqueness?
    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
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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:
 
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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 and 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.
Back to Top
jradxl View Drop Down
Newbie
Newbie


Joined: 14-Jun-2011
Posts: 30
Post Options Post Options   Quote jradxl Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down