C# 我正在尝试使用存储库模式和MVC获取数据库中提交的最后一条记录
我正在尝试使用存储库模式和MVC获取数据库中提交的最后一条记录。我附加了接口和类。还有控制器,你可以把代码放在那里。如果你需要更多的细节,请告诉我。谢谢C# 我正在尝试使用存储库模式和MVC获取数据库中提交的最后一条记录,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我正在尝试使用存储库模式和MVC获取数据库中提交的最后一条记录。我附加了接口和类。还有控制器,你可以把代码放在那里。如果你需要更多的细节,请告诉我。谢谢 public interface IRequestRepository { tblRequest GetCaseId(int caseId); } public class RequestRepository: IRequestRepository { helpdeskEntities context = null;
public interface IRequestRepository
{
tblRequest GetCaseId(int caseId);
}
public class RequestRepository: IRequestRepository
{
helpdeskEntities context = null;
public RequestRepository()
{
context = new helpdeskEntities();
}
public string GetCaseId(Ticket ticket)
{
string caseId = string.Empty;
tblRequest tr = context.tblRequests.Where(u => u.CaseID == ticket.CaseID && u.UEmailAddress == ticket.UEmailAddress).SingleOrDefault();
if (tr != null)
{
caseId = tr.CaseID;
}
return caseId;
}
}
public class Ticket
{
public int CaseID { get; set; }
public string Title { get; set; }
[Required]
public string UFirstName { get; set; }
[Required]
public string ULastName { get; set; }
//public string UDisplayName { get; set; }
[Required]
public string UDep_Location { get; set; }
[Required]
public string UEmailAddress { get; set; }
//public string UComputerName { get; set; }
//public string UIPAddress { get; set; }
[Required]
public string UPhoneNumber { get; set; }
[Required]
public string Priority { get; set; }
[Required]
public string ProbCat { get; set; }
//public string IniDateTime { get; set; }
//public string UpdateProbDetails { get; set; }
//public string UpdatedBy { get; set; }
public string InitiatedBy_tech { get; set; }
public string AssignedBy { get; set; }
public string TechAssigned { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string ProbDetails { get; set; }
}
Controller
public ActionResult CreateTicket(tblRequest td)
{
}
首先,您需要升级iRequestRespository并添加该方法: (我假设您正在为此使用EntityFramework) 接下来,您需要在
RequestRepository
中实现该方法:
public class RequestRepository : IRequestRepository
{
/* other code here */
public Request Latest(Ticket ticket)
{
// I'm also assuming you're using an auto incremented CaseId
return this.context.tblRequests.OrderByDescending(p => p.CaseId).FirstOrDefault(p => p.UEmailAddress == ticket.UEmailAddress);
}
}
还有一件事: 您的IRequestRepository.GetCaseId实现返回一个字符串,而它应该返回一个
tblRequest
(您也可以期望它返回一个int-Id…)
不管怎样,我希望这有帮助 在RequestRepository类中实现IRequestRepository接口。在GetCaseId方法中使用Ticket作为参数不是接口声明的内容。
public class RequestRepository : IRequestRepository
{
/* other code here */
public Request Latest(Ticket ticket)
{
// I'm also assuming you're using an auto incremented CaseId
return this.context.tblRequests.OrderByDescending(p => p.CaseId).FirstOrDefault(p => p.UEmailAddress == ticket.UEmailAddress);
}
}