更新模型导致在使用实体框架的努力时从DbContext中删除接口

本文关键字:努力 DbContext 接口 删除 框架 模型 实体 更新 | 更新日期: 2023-09-27 18:13:17

目前,我正试图使用努力(https://effort.codeplex.com/)与我的实体框架6解决方案,允许单元测试,而不需要数据库连接(见http://www.codeproject.com/Articles/460175/Two-strategies-for-testing-Entity-Framework-Effort)。一切都在我的项目中工作,其中这是具有接口的DbContext和工作所需的重载构造函数:

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

问题是,如果我通过选择"从数据库更新模型…"来更新.edmx文件,那么它将重新生成上下文文件为:

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

所以我必须回去,每次手动更新上述Context.cs文件。另外,从POCO CCS_DEPT_TBL文件中删除[Key]

是否有任何方法来设置我的实体框架项目,使它不会吹走接口和重载构造函数,每次我从数据库更新模型?TIA。

更新:

由于类是局部的,我只是确保接口和重载构造函数放在一个单独的文件中,该文件不是自动生成的。

UPDATE 2:

OK,得到了它,只是将其添加为一个单独的文件,将DEPTID从原始POCO文件中取出,但它仍然将DEPTID放在生成的文件中,因此在更新后构建中断,因为在同一类中有两个DEPTID值:

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

那么,我如何防止它在生成的类文件中生成DEPTID,因为它已经在上面的部分类文件中?

更新模型导致在使用实体框架的努力时从DbContext中删除接口

是的,这个类被定义为分部类。创建一个新文件,声明相同的分部类,并在其中添加额外的方法。

对于[Key]属性丢失,您可以尝试使用MetadataType属性并将所有元数据放在那里。

namespace Cssd.IT.PortalIntegration.DataAccess.HR.Dao
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    [MetadataType(typeof(CCS_DEPT_TBL_Meta))]
    public partial class CCS_DEPT_TBL
    {
      ... Your additional constructors and methods here ...
    }
    public class CCS_DEPT_TBL_Meta
    {
        [Key]
        public string DEPTID { get; set; }    
    }
}

谢谢@Robert McKee!下面是我最后做的:

CCS_DEPT_TBL_Key.cs:

namespace Cssd.IT.PortalIntegration.DataAccess.HR.Dao
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    [MetadataType(typeof(CCS_DEPT_TBL_Meta))]
    public partial class CCS_DEPT_TBL
    {
    }
    public class CCS_DEPT_TBL_Meta
    {
        [Key]
        public string DEPTID { get; set; }    
    }
}

CCS_DEPT_TBL.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 Cssd.IT.PortalIntegration.DataAccess.HR.Dao
{
    using System;
    using System.Collections.Generic;
    public partial class CCS_DEPT_TBL
    {
        // This table in SQL Server does not have a primary key, it just has an index
        public string DEPTID { get; set; }
        public string DESCR { get; set; }
        public System.DateTime EFFDT { get; set; }
        public string EFF_STATUS { get; set; }
        public Nullable<System.DateTime> LASTUPDDTTM { get; set; }
    }
}

HRModel.Context.cs: HRADDbContext实际上是数据库优先的实体,但是还有另外三个*。edmx文件是同一项目中代码优先的实体,所以在HRModel.Context.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 Cssd.IT.PortalIntegration.DataAccess.HR.Dao
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    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; }
    }
}

HRADDbContext.cs,其中IHRADDbContext接口对于Effort是必需的:

using System;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
namespace Cssd.IT.PortalIntegration.DataAccess.HR.Dao
{
    /// <summary>
    /// Added interface here so that it does not get removed when updating 
    /// model from the database on code generation.
    /// </summary>
    partial class HRADDbContext : IHRADDbContext
    {
        public HRADDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            this.Configuration.LazyLoadingEnabled = false;
        }
        public HRADDbContext(DbConnection connection)
            : base(connection, true)
        {
            this.Configuration.LazyLoadingEnabled = false;
        }
    }
}