简体   繁体   中英

Set UseDatabaseNullSemantics for generated DbContext EF6

I have an EF6 DbContext that is auto generated from a model on to which I would like to set UseDatabaseNullSemantics to true.

I don't want to set UseDatabaseNullSemantics the value on each instance of the DbContext (that would mean to much code) and since it is generated I can't set UseDatabaseNullSemantics the value in the DbContext's constructor.

Is there a way for me to set UseDatabaseNullSemantics to true for the application once and for all?

Edit + Clarification:

My "FordonTrpEntities" DbContext subclass is autogenerated and located in/under the "FordontTrp.edmx" model file.

在此处输入图片说明

If I set the configuration in the constructor of this class won't it be erased if/when the file is regenerated? As stated in the file header:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
public partial class FordonTrpEntities : DbContext
{
    public FordonTrpEntities()
        : base("name=FordonTrpEntities")
    {
        Configuration.UseDatabaseNullSemantics = true;
    }

Yes, you have to set it in the constructor of the DbContext class like this:

public class YourDbContext : DbContext 
{
     public YourDbContext() 
     {
          Configuration.UseDatabaseNullSemantics = true;
     }

     // DbSet declarations
}

You'll notice a huge performance improvement in most of the queries where you have filters as it will not add null checks to the conditions.

If you are working with auto-generated code, you cannot modify this class directly because it will be overriden. In that case you can modify the TT file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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