如何向ADO.NET实体添加约束? [英] How can I add constraints to an ADO.NET Entity?

查看:164
本文介绍了如何向ADO.NET实体添加约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将一组字段标记为ADO.NET实体的主键,但是我没有找到一种方法来声明唯一约束或检查约束。



设计师或框架上缺少此功能?

解决方案

ADO中不存在对唯一键/约束的支持。 v4.0中的NET实体,请参阅具有独特约束的外键的一对一关联,其中Diego B Vega说:


我知道,我确信我们没有为4.0中的
主键添加
支持。




但是,他提供了一个可能的解决方法/黑客(所有正常的注意事项):


你可能意识到,这是
通常可能谎言到实体
框架并在SSDL中告诉它,对于
实例,一些唯一的键是
主键。我认为如果实际的主键是
代理键(即为此
目的添加了一个IDENTITY
列),而且您甚至不需要必须
将其映射到模型中。



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?

解决方案

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.

这篇关于如何向ADO.NET实体添加约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆