현재 Entity Framework 6 솔루션과 함께 Effort ( https://effort.codeplex.com/ )를 사용하여 데이터베이스 연결없이 단위 테스트를 수행하려고합니다 ( http://www.codeproject.com/Articles/460175 참조) . / 테스트를위한 두 가지 전략 - 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]
를 제거합니다.
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; }
}
}
고맙습니다 @ 로버트 맥키! 내가 끝내었던 것은 다음과 같다 :
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;
}
}
}