在我的项目中,我在 mvc 控制器中编写了这样的 maxId--
public class PurchaseOrderController : Controller
{
private IPurchaseOrder interfaceRepository;
public PurchaseOrderController()
{
if (interfaceRepository==null)
{
this.interfaceRepository= new Repository();
}
}
int maxId=interfaceRepository.GetMaxPK(a=>a.POMSTID);
}
这里
a=>a.POMSTID
是您要查询的列选择器。假设您要选择名为“PODETID”的列,那么列选择器将是这样的--
a=>a.PODETID
现在在界面中--
public interface IPurchaseOrder:IRepository<PURCHASEORDERMASTER>
{
}
这里的“PURCHASEORDERMASTER”是我的查询表或 TEntity。
public interface IRepository<TEntity> where TEntity : class
{
int GetMaxPK(Func<TEntity, decimal> columnSelector);
}
现在在存储库(或具体)类(继承接口)中,您必须在其中调用数据库,您必须像这样编写--
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
public int GetMaxPK(Func<TEntity, decimal> columnSelector)
{
var GetMaxId = db.Set<TEntity>().Max(columnSelector);
return GetMaxId;
}
}
这里 db 是数据库上下文,decimal 是我的查询列的数据类型。