views:

487

answers:

2

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?

Thanx.

+1  A: 

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.

casperOne
A: 

Would someone please clarify the workaround? Does it allow this scenario:

Table A                    Table B                 Table C
IdentityCol < 1 --- 0..1 > UniqueConst
                           IdentityCol < 1 --- etc.

Because 2 things I run across trying Diego B Vega's approach in the URL:

  1. It breaks the relationship from B-to-C while fixing A-to-B
  2. It doesn't compile when I change Schema/EntityType[TableB]/Key/PropertyRef

Do I misunderstand the solution, or does it just not work if Table B isn't a terminal point?

Thanks, Shannon

p.s. The more I think about this, the less sense it makes. So EF will think I can add a row that violates the unique constraint to Table B, but fault on save? What exactly am I being protected from again?

Shannon