我正在学习使用泛型和依赖注入的存储库和工作单元模式。我有一个持续的错误,我一直在搅动,而且我显然错过了一些基本的东西。依赖注入和泛型基类错误
我收到以下错误,而据我所知,它正在寻找所谓的“ID”
数据库表列,因为我的实体类从BaseEntity继承我怀疑这是问题,但我不明白为什么和我最好做什么来解决它。
public class BaseEntity<T>
{
public T Id { get; set; }
}
返回错误
类型 'System.Data.Entity.Core.EntityCommandExecutionException'>的异常出现在EntityFramework.SqlServer.dll但在用户代码中没有处理
附加信息:执行命令>定义时发生错误。详情请参阅内部例外。
内部异常
{ “无效列名 '身份证'。”}
要添加一些背景我使用EF6 MVC5和AutoFac为扶养注射。
实体类 (A柱“身份证”不存在于数据库 - 在数据库中的关键是“EmrgencyAttendanceId”)和实体类也可将密钥像这样“EmrgencyAttendanceId”。
[Table("reporting.EDISRecords")]
public class EDISRecord : BaseEntity<int>
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EmergencyAttendanceId { get; set; }
[StringLength(50)]
public string Hospital { get; set; }
[StringLength(20)]
public string URNumber { get; set; }
控制器动作 控制器调用EDISRecordService的GetRecord方法传递一个Lambda表达式。我使用DbFunctions.TruncateTime,因为数据库存储为日期时间,我只想在日期上进行搜索。
public ActionResult Search(string Date, string CaseNumber)
{
if (!string.IsNullOrEmpty(Date) || !string.IsNullOrEmpty(CaseNumber))
{
DateTime dt = DateTime.Parse(Date);
var EmergencyAttendance = _edisRecordService.GetRecord(m => (DbFunctions.TruncateTime(m.ArrivalDateTime) == dt) && (m.RTAIdentifier == CaseNumber));
//for initialising view model
SeperationSummaryViewModel model = new SeperationSummaryViewModel();
//assign values for view model
if (EmergencyAttendance != null)
{
if (EmergencyAttendance.DepartureDestination != null)
{
if (EmergencyAttendance.DepartureDestination.Substring(0, 1) == ".")
{
model.DepartureDestination = EmergencyAttendance.DepartureDestination.Substring(1);
}
else
{
model.DepartureDestination = EmergencyAttendance.DepartureDestination;
}
}
else
{
model.DepartureDestination = "Not recorded by Emergency Department";
}
if (EmergencyAttendance.InpatientAdmissionDiagnosis != null)
{
model.InpatientAdmissionDiagnosis = EmergencyAttendance.InpatientAdmissionDiagnosis;
}
else
{
model.InpatientAdmissionDiagnosis = "Not recorded by Emergency Department";
}
}
//send view model into UI (View)
return PartialView("_SeperationInformationPartialView", model);
}
else
{
if (string.IsNullOrEmpty(Date) || string.IsNullOrEmpty(CaseNumber))
{
return PartialView("Blank");
}
}
return PartialView("Error");
}
服务类 服务类实例化的一个工作单元,并调用存储库获取方法通过Lambda表达式。
public class EDISRecordService : IEDISRecordService
{
private readonly IUnitOfWork<DataWarehouseDataManager> _unitOfWork;
public EDISRecordService(IUnitOfWork<DataWarehouseDataManager> unitOfWork)
{
_unitOfWork = unitOfWork;
}
public EDISRecord GetRecord(Expression<Func<EDISRecord, bool>> @where)
{
return _unitOfWork.EDISRecordRepository.Get(@where);
}
}
RepositoryBase类 存储库类从仓库基类impliments Get方法传递lambda来 '表达式> @where'
public class RepositoryBase<TEntity> : Disposable, IRepository<TEntity>
where TEntity : class
{
private readonly DbContext _dataContext;
private IDbSet<TEntity> Dbset
{
get { return _dataContext.Set<TEntity>(); }
}
public RepositoryBase(DbContext dbContext)
{
_dataContext = dbContext;
}
public TEntity Get(Expression<Func<TEntity, bool>> @where)
{
return Dbset.Where(where).FirstOrDefault();
}
protected override void DisposeCore()
{
if (_dataContext != null)
_dataContext.Dispose();
}
}
它无法在该方法中继承
public TEntity Get(Expression<Func<TEntity, bool>> @where)
{
return Dbset.Where(where).FirstOrDefault(); <<<<<< Fails Here <<<<<
}