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

╄→гoц情女王★ 提交于 2019-12-18 05:55:54

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/1963829/how-can-i-add-constraints-to-an-ado-net-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!