如何返回 EF4 通用存储库中最后一个增值主键?
我尝试使用 Ef 4 编写存储库,但出现问题。我需要返回数据库返回 ID 中最后添加的行。例如:我添加了姓名、姓氏,但无法访问最后添加的主要 ID。我的存储库如下。我的要求是二次使用。
using System.Data.Objects;
namespace FlyAnt.DAL.FrameWork.ApplicationDomainModel
{
public interface IDomainRepository<Tmodel> : IDisposable where Tmodel : class
{
IQueryable<Tmodel> Fetch();
IEnumerable<Tmodel> GetAll();
IEnumerable<Tmodel> Find(Func<Tmodel, bool> predicate);
Tmodel Single(Func<Tmodel, bool> predicate);
Tmodel First(Func<Tmodel, bool> predicate);
bool Add(Tmodel entity);
bool Delete(Tmodel entity);
bool Attach(Tmodel entity);
}
public class DomainRepository<TModel> : IDomainRepository<TModel> where TModel : class
{
#region IDomainRepository<T> Members
private ObjectContext _context;
private IObjectSet<TModel> _objectSet;
public DomainRepository()
{
}
public DomainRepository(ObjectContext context)
{
_context = context;
_objectSet = _context.CreateObjectSet<TModel>();
}
. . . . .
. . .
. .
.
public bool Delete(Func<TModel, bool> predicate)
{
IEnumerable<TModel> records = from x in _objectSet.Where<TModel>(predicate) select x;
foreach (TModel record in records)
_objectSet.DeleteObject(record);
return SaveChanges()>0;
}
public bool Add(TModel entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
_objectSet.AddObject(entity);
return SaveChanges()>0;
}
...............
..........
......// do someting
第二次用法:我如何返回最后添加或最后删除的 ID?我尝试如下:(
public bool Delete(Func<TModel, bool> predicate)
{
IEnumerable<TModel> records = from x in _objectSet.Where<TModel>(predicate) select x;
foreach (TModel record in records)
_objectSet.DeleteObject(record);
return _objectset.LASTDELETEDPrimaryKEY;
}
public bool Add(TModel entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
_objectSet.AddObject(entity);
return _objectset.LASTADDEDPrimaryKEY;
}
最好的祝福...
i try to write a repository with Ef 4, but there is a problem. i need return last added row in database return id. for exmple: i added Name, SurName but i can not access last added primary id. My Repository is below. my request is secand usage.
using System.Data.Objects;
namespace FlyAnt.DAL.FrameWork.ApplicationDomainModel
{
public interface IDomainRepository<Tmodel> : IDisposable where Tmodel : class
{
IQueryable<Tmodel> Fetch();
IEnumerable<Tmodel> GetAll();
IEnumerable<Tmodel> Find(Func<Tmodel, bool> predicate);
Tmodel Single(Func<Tmodel, bool> predicate);
Tmodel First(Func<Tmodel, bool> predicate);
bool Add(Tmodel entity);
bool Delete(Tmodel entity);
bool Attach(Tmodel entity);
}
public class DomainRepository<TModel> : IDomainRepository<TModel> where TModel : class
{
#region IDomainRepository<T> Members
private ObjectContext _context;
private IObjectSet<TModel> _objectSet;
public DomainRepository()
{
}
public DomainRepository(ObjectContext context)
{
_context = context;
_objectSet = _context.CreateObjectSet<TModel>();
}
. . . . .
. . .
. .
.
public bool Delete(Func<TModel, bool> predicate)
{
IEnumerable<TModel> records = from x in _objectSet.Where<TModel>(predicate) select x;
foreach (TModel record in records)
_objectSet.DeleteObject(record);
return SaveChanges()>0;
}
public bool Add(TModel entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
_objectSet.AddObject(entity);
return SaveChanges()>0;
}
...............
..........
......// do someting
SECAND USAGE:how can i return last added or last deleted ID?i try to make below :(
public bool Delete(Func<TModel, bool> predicate)
{
IEnumerable<TModel> records = from x in _objectSet.Where<TModel>(predicate) select x;
foreach (TModel record in records)
_objectSet.DeleteObject(record);
return _objectset.LASTDELETEDPrimaryKEY;
}
public bool Add(TModel entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
_objectSet.AddObject(entity);
return _objectset.LASTADDEDPrimaryKEY;
}
Best Regards...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很快你就可以使用
SaveChanges()
了。如果你想添加,让我们说值到表,并且一个依赖于另一个,让我们假设:
你可以轻松地:
soon you do
SaveChanges()
all will be right there to use.if you want to add, let's say values to to tables and one depends on the other, let's assume:
you can easily: