目前,我正在嘗試使用Effort( https://effort.codeplex.com/ )和我的Entity Framework 6解決方案來進行單元測試而無需數據庫連接(請參閱http://www.codeproject.com/Articles/460175 /兩個策略 - 測試 - 實體 - 框架 - 努力 )。一切都在我的項目中工作,這是帶有接口的DbContext
和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; }
}
}
問題是,如果我通過選擇“從數據庫更新模型...”來更新.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]
。
有沒有辦法設置我的Entity Framework項目,以便每次我從數據庫更新模型時它都不會吹掉接口和重載的構造函數? TIA。
更新:
由於該類是部分的,我只是確保將接口和重載的構造函數放在一個不自動生成的單獨文件中。
更新2:
好了,得到它,只是將它作為一個單獨的文件添加,將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
,因為它已經在上面的部分類文件中了?
是的,該類被定義為部分類。創建一個新文件,該文件也聲明了相同的分部類,並在那裡添加其他方法。
至於丟失的[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,其中Effort需要IHRADDbContext接口:
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;
}
}
}