x3m
x3m

Reputation: 553

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.

enter image description here

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;
    }

Upvotes: 1

Views: 2446

Answers (1)

Francisco Goldenstein
Francisco Goldenstein

Reputation: 13767

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.

Upvotes: 2

Related Questions