I have a table that need both a PK and a UC, each being a different column. I understand that EF 5.0 does not have the ability to make UC and no plans are in the work to add it any times soon. So I am stuck on figuring out how to make the equivalant to a
UC in EF. It is appsolutly imparitive that I can make sure that a certain the column does not contain repeat information in it.
I want to setup a way either at the DB level or at the app level to force a column so it can only contain uniquie entries but I can not use a GUID, it needs to be a string provided by me (kind of like having an employee ID as PK and then SSN as UC) If I
can just mod the DB after/during creation to have the UC attached to it (I am using SQL 2012) that is fine but will EF know how to deal with an error through by SQL when the UC is violated?
In my mind, I think you can just make up to your constructor in your EntityFramework's Entity Model, a typcial model sample should be this:
public partial class EntityModel
{
…………
public EntityModel()
{
Property1 = Value1; //Your Property should be Guid, and use Value=Guid.NewGuid()
}
}
I know how to make class and as stated I can't use a GUID. The value must be supplied by me and not the computer. You would not use a GUID for a SSN field.
I know how to make class and as stated I can't use a GUID. The value must be supplied by me and not the computer. You would not use a GUID for a SSN field.
Then I think you have two ways:
1) Define a static variable that will be increased by 1 each time:
public partial class EntityModel
{
private static int _autoNum = 0;
public EntityModel()
{
Property1 = (++_auotNum);
}
}
Now let me explain to you in detail, if anything wrong,please correct me kindly;)
I mean that if you do as what I tell you above, the primary key will be auotmatically created and you can use that directly. And when you want to use the foreign key, no need to know about the primary key but just assign the primary table's instance to the
Navigator of foreign table (One Part) itself it will be done by EF5.
Eagle_f90
Member
465 Points
531 Posts
how to make sure data is unique in a column using EF5?
Nov 29, 2012 02:47 AM|LINK
I have a table that need both a PK and a UC, each being a different column. I understand that EF 5.0 does not have the ability to make UC and no plans are in the work to add it any times soon. So I am stuck on figuring out how to make the equivalant to a UC in EF. It is appsolutly imparitive that I can make sure that a certain the column does not contain repeat information in it.
DarrellNorto...
All-Star
86555 Points
9624 Posts
Moderator
MVP
Re: how to make sure data is unique in a column using EF5?
Nov 29, 2012 09:24 AM|LINK
Are you trying to figure out how to model a unique constraint using code-first? If so, just create an ExecuteStoreCommand to create the unique constraint in the DatabaseInitializer. Example: http://stackoverflow.com/questions/4413084/unique-constraint-in-entity-framework-code-first
Or are you trying to figure out how your application could create/set the unique value in the model, then pass that to the database? In that case you could use a Guid. EF supports Guids and they are almost always guaranteed to be unique. Example: http://itzkris444.wordpress.com/2011/10/03/configuring-primary-key-type-as-guid-entity-framework-code-first-approach/
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Eagle_f90
Member
465 Points
531 Posts
Re: how to make sure data is unique in a column using EF5?
Nov 29, 2012 11:11 AM|LINK
I want to setup a way either at the DB level or at the app level to force a column so it can only contain uniquie entries but I can not use a GUID, it needs to be a string provided by me (kind of like having an employee ID as PK and then SSN as UC) If I can just mod the DB after/during creation to have the UC attached to it (I am using SQL 2012) that is fine but will EF know how to deal with an error through by SQL when the UC is violated?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to make sure data is unique in a column using EF5?
Nov 30, 2012 04:01 AM|LINK
Hello Eagle,
In my mind, I think you can just make up to your constructor in your EntityFramework's Entity Model, a typcial model sample should be this:
public partial class EntityModel { ………… public EntityModel() { Property1 = Value1; //Your Property should be Guid, and use Value=Guid.NewGuid() } }Eagle_f90
Member
465 Points
531 Posts
Re: how to make sure data is unique in a column using EF5?
Nov 30, 2012 10:55 AM|LINK
I know how to make class and as stated I can't use a GUID. The value must be supplied by me and not the computer. You would not use a GUID for a SSN field.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to make sure data is unique in a column using EF5?
Dec 01, 2012 01:58 AM|LINK
Then I think you have two ways:
1) Define a static variable that will be increased by 1 each time:
public partial class EntityModel { private static int _autoNum = 0; public EntityModel() { Property1 = (++_auotNum); } }2)Manunally assign variable and its value.
Eagle_f90
Member
465 Points
531 Posts
Re: how to make sure data is unique in a column using EF5?
Dec 03, 2012 03:25 PM|LINK
The data will be assiend by me but how can I make sure that EF will through a UC violation if duplicate data is entered
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to make sure data is unique in a column using EF5?
Dec 04, 2012 12:28 AM|LINK
since you key is identitied by codes. No need to check.
Eagle_f90
Member
465 Points
531 Posts
Re: how to make sure data is unique in a column using EF5?
Dec 04, 2012 12:41 AM|LINK
And that brings us back to my first quesytion, how to mark a column as a uc. I can only find info on PK and FK in EF
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: how to make sure data is unique in a column using EF5?
Dec 04, 2012 01:21 AM|LINK
Hello again,
Interesting……
Now let me explain to you in detail, if anything wrong,please correct me kindly;)
I mean that if you do as what I tell you above, the primary key will be auotmatically created and you can use that directly. And when you want to use the foreign key, no need to know about the primary key but just assign the primary table's instance to the Navigator of foreign table (One Part) itself it will be done by EF5.