Presently, I am attempting to use Effort (https://effort.codeplex.com/) with my Entity Framework 6 solution to allow unit testing without requiring a database connection (see http://www.codeproject.com/Articles/460175/Two-strategies-for-testing-Entity-Framework-Effort). Everything works in my project, where this is the DbContext
with an interface and the overloaded constructors required for Effort:
namespace Cssd.IT.PortalIntegration.DataAccess.HR.Dao
{
using System;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class HRADDbContext : DbContext, IHRADDbContext
{
public HRADDbContext() : base("name=HRADDbContext")
{
}
public HRADDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
this.Configuration.LazyLoadingEnabled = false;
}
public HRADDbContext(DbConnection connection)
: base(connection, true)
{
this.Configuration.LazyLoadingEnabled = false;
}
public virtual DbSet<CCS_DEPT_TBL> CCS_DEPT_TBL { get; set; }
public virtual DbSet<CCS_HR_AD_SYNC> CCS_HR_AD_SYNC { get; set; }
}
}
The problem is, if I update the .edmx
file by choosing "Update model from database..." then it regenerates the context file to be:
namespace Cssd.IT.PortalIntegration.DataAccess.HR.Dao
{
using System;
using System.Data.Common;
using System.Data.Entity;
public partial class HRADDbContext : DbContext
{
public HRADDbContext() : base("name=HRADDbContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<CCS_DEPT_TBL> CCS_DEPT_TBL { get; set; }
public virtual DbSet<CCS_HR_AD_SYNC> CCS_HR_AD_SYNC { get; set; }
}
}
So I have to go back and manually update the above Context.cs
file every time. Also, removes the [Key]
from the POCO CCS_DEPT_TBL
file.
Is there any way to setup my Entity Framework project so that it doesn't blow away the interface and overloaded constructors every time I update the model from the database? TIA.
UPDATE:
Since the class is partial, I just ensure that the interface and overloaded constructors are put in a separate file that is not auto-generated.
UPDATE 2:
OK, got it, just added this as a separate file, taking the DEPTID
out of the original POCO
file, but it still puts that DEPTID
in the generated file, so after an update the build breaks because there are two DEPTID
values in the same class:
namespace Cssd.IT.PortalIntegration.DataAccess.HR.Dao
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class CCS_DEPT_TBL
{
[Key]
public string DEPTID { get; set; }
}
}
So, how do I prevent it from generating DEPTID
in the generated class file, since it is already in the above partial class file?