Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add constraints to an ADO.NET Entity?

I know how to mark a group of fields as primary key in ADO.NET entities but i haven't found a way to declare unique constraints or check constraints.

Is this feature missing on the designer or on the framework?

like image 821
angrifel Avatar asked Dec 26 '09 17:12

angrifel


1 Answers

Support for unique keys/constraints does not exist in ADO.NET Entities in v4.0, see the answer to "one-to-one association on a foreign key with unique constraint", where Diego B Vega says:

I know for sure we haven't added support for unique keys other than primary keys in 4.0.

He does, however, provide a possible workaround/hack (which comes with all the normal caveats):

As you are probably aware of, it is often possible to “lie” to Entity Framework and tell it in the SSDL, for instance, that some unique key is the primary key. I reckon this would work very well if the actual primary key is an surrogate key (i.e. an IDENTITY column that was added for this purpose) and you don’t even have to map it in the model.

like image 200
casperOne Avatar answered Oct 14 '22 06:10

casperOne