1
Vote

Have to manually update context class every time

description

When using Effort, it requires an interface to be used in the context class, such as in MyAppContext.cs:
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

namespace TestingEf.Data.Entities
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Common;
    
    public partial class MyAppContext : DbContext, IMyAppContext
    {
        /// <summary>
        /// Constructs a new context instance with the default connection string.
        /// </summary>
        public MyAppContext()
            : base("name=MyAppContext")
        {
            this.Configuration.LazyLoadingEnabled = false;
        }
    
        /// <summary>
        /// Constructs a new context instance using the given string as the name or connection
        /// string for the database to which a connection will be made.  See the DbContext class
        /// remarks for how this is used to create a connection.
        /// </summary>
        /// <param name="nameOrConnectionString">Either the database name or a connection string.</param>
        public MyAppContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            this.Configuration.LazyLoadingEnabled = false;
        }
    
        /// <summary>
        /// Constructs a new context instance using the given DbConnection.
        /// The given connection will be disposed when this instance is disposed
        /// </summary>
        /// <param name="connection">The connection to create the context with</param>
        public MyAppContext(DbConnection connection)
            : base(connection, true)
        {
            this.Configuration.LazyLoadingEnabled = false;
        }
    
        public IDbSet<Product> Products { get; set; }
        public IDbSet<ProductTag> ProductTags { get; set; }
        public IDbSet<Tag> Tags { get; set; }
    
    }
}
The problem is, every time I update the EDMX model from changes in the SQL Server database, it also re-generates the MyAppContext.cs file, so the IMyAppContext gets blown away:

Update Model

Is there any way to resolve this issue so that I don't have to re-add the IMyAppContext and the overloaded constructors every time I update the EDMX model from the database?

comments

ibrahimbensalah wrote Jan 11, 2016 at 10:27 AM

You can split your code into two files. So you need to create another new file with the following code:

namespace TestingEf.Data.Entities
{
  partial class MyAppContext : DbContext, IMyAppContext
  {
    /// <summary>
    /// Constructs a new context instance using the given DbConnection.
    /// The given connection will be disposed when this instance is disposed
    /// </summary>
    /// <param name="connection">The connection to create the context with</param>
    public MyAppContext(DbConnection connection)
        : base(connection, true)
    {
        this.Configuration.LazyLoadingEnabled = false;
    }
  }
}