简体   繁体   English

ef6 include()用于具有必需属性的可空属性

[英]ef6 include() for nullable properties with required attribute

We've just updated to the EF6 and have an issue with a strange behavior comparing to the previous version. 我们刚刚更新到EF6,与以前的版本相比,存在一个行为异常的问题。

EF used to generate LEFT JOINs for the Include statement if the property was nullable. 如果属性可为空,则EF用于为Include语句生成LEFT JOIN。 However it looks like this version takes into account also the [required] attribute. 但是,此版本看起来也考虑了[required]属性。

For example I have the following class: 例如,我有以下课程:

public class Client
{    
    [DisplayName("Industry Code")]
    [Required]
    public int? IndustryId { get; set; }

    [ForeignKey("IndustryId")]
    public Industry IndustryEntity { get; set; }
}

When we do 当我们做

return db.Clients
    .Include(o => o.CountryISOEntity)
        .... 
    .Include(o => o.IndustryEntity)
    .Single(o => o.Id == clientId);

the previous version generated LEFT OUTER JOIN, but the new one generates INNER JOIN. 以前的版本生成了LEFT OUTER JOIN,而新版本生成了INNER JOIN。

If I remove the [required] attribute than it generates the LEFT JOIN. 如果删除[required]属性,它将生成LEFT JOIN。 But it's not an option in my case as we use this attribute to show the errors to the user, however the user has an option to save the incomplete entity and to return to it later. 但这不是我的选择,因为我们使用此属性向用户显示错误,但是用户可以选择保存不完整的实体,然后再返回。 And when the user comes back it get an error that the record doesn't exist as the include generates INNER JOIN. 当用户返回时,会收到一条错误消息,指出该记录不存在,因为include会生成INNER JOIN。

Is there a setting in the new EF6 to disable this behavior (to ignore the attributes) and to generate sql query based on the nullable information? 新EF6中是否有设置可禁用此行为(忽略属性)并基于可为空的信息生成sql查询? Or can I force the LEFT JOIN in the include? 还是可以在包含中强制使用LEFT JOIN? Or is our only option to rollback to the previous EF version? 还是我们唯一的回滚到以前的EF版本的选择?

PS: we are using .Net 4.0 PS:我们正在使用.Net 4.0

Thanks 谢谢

I assume you want to use the Required attribute for form validation in MVC, not to tell Entity Framework that the property is required. 我假设您要在MVC中使用Required属性进行表单验证,而不是告诉Entity Framework该属性是必需的。 This is why you need view models, so you can separate validation logic of views from Entity Framework validation logic. 这就是为什么需要视图模型的原因,因此可以将视图的验证逻辑与Entity Framework验证逻辑分开。

Your view models represents a view and contains all the data that is needed to render the view. 您的视图模型代表一个视图,并包含呈现该视图所需的所有数据。 A domain model (entity) represents one (or more) table(s) in the database. 域模型(实体)表示数据库中的一个(或多个)表。 View models don't necessarily contain the same properties as the domain model. 视图模型不一定包含与域模型相同的属性。 In my personal experience they are usually a combination of different domain models. 以我的个人经验,它们通常是不同领域模型的组合。

If you create a view model for a Client , you can remove the validation attributes from your EF POCO. 如果为Client创建视图模型,则可以从EF POCO中删除验证属性。

The view model will look like this: 视图模型将如下所示:

public class ClientModel
{
    [DisplayName("Industry Code")]
    [Required]
    public int IndustryId { get; set; }

    // Other properties..
}

Then your POCO can look like this: 然后,您的POCO可能如下所示:

public class Client
{
    public int? IndustryId { get; set; }

    [ForeignKey("IndustryId")]
    public Industry IndustryEntity { get; set; }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM