简体   繁体   English

条件两个属性之一必须为null Entity Framework Code First

[英]Condition one of two properties to be different of null Entity Framework Code First

I am using EF6 Code First. 我正在使用EF6 Code First。 Suppose the following scenario. 假设以下情况。 We have this class: 我们有这个课:

class Foo
{
    public int Id { get; set; }
    public A AProperty { get; set; }
    public B BProperty { get; set; }
}

Is there a way, by using either Data Annotations or Entity Framework Fluent Api and without redesigning the whole model, to ensure that any Foo entity must have either at least one property different of null but not both? 是否有办法通过使用数据注释或实体框架Fluent Api而无需重新设计整个模型来确保任何Foo实体必须至少具有一个不同于null的属性,而不能同时具有两者?

It is something like what is answered in this question but with EF. 这个问题的答案与EF类似。

我认为您必须在其他层(例如您的业务层)中进行此验证,而不是将此职责委托给ef

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码优先导航属性返回空字段 - Entity Framework Code First navigational properties returning null fields 实体框架代码前两个导航属性到/来自一个抽象实体 - Entity Framework Code First Two navigationProperty to/from one abstract entity 实体框架代码首先迁移两个不同的数据库 - Entity Framework code first migrations for two different databases 实体框架代码优先 - 两个具有相同名称但位于不同名称空间的实体 - Entity Framework Code First - two entities with same name but in different namespaces 实体框架代码优先:两个DbContext和一个数据库初始化程序 - Entity Framework Code First : Two DbContext and one database initializers 实体框架代码优先 - 将两个字段联合成一个集合 - entity framework code first - Union of the two fields into one collection 首先在实体框架代码中连接具有一对多关系的两个表 - Joining two tables with one to many relatipnship in entity framework code first Entity Framework Core - 代码优先 - 两个外键 - 一张表 - Entity Framework Core - Code First - Two Foreign Keys - One Table 实体框架6代码首先处理阴影属性 - Entity Framework 6 code first handing shadow properties 实体框架:导航属性-代码优先 - Entity Framework: Navigational properties - Code First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM