如果有外键(EF-4),如何使用通用存储库添加新值?
我尝试编写一种通用存储库来添加方法。 一切都可以添加,但我有一个与两个带有外键的表相关的表。但由于外键而无法工作
public class DomainRepository<TModel> : IDomainRepository<TModel> where TModel : class
{
private ObjectContext _context;
private IObjectSet<TModel> _objectSet;
public DomainRepository(ObjectContext context)
{
_context = context;
_objectSet = _context.CreateObjectSet<TModel>();
}
// do something...
public TModel Add<TModel>(TModel entity) where TModel : IEntityWithKey
{
EntityKey key = _context.CreateEntityKey(entity.GetType().Name, entity);
_context.AddObject(key.EntitySetName, entity);
_context.SaveChanges();
return entity;
}
// do something...
}
调用存储库:
// insert-update-delete
public partial class AddtoTables
{
public table3 Add(int TaskId, int RefAircraftsId)
{
using (DomainRepository<table3> repTask = new DomainRepository<table3>(new TaskEntities()))
{
return repTask.Add<table3>(new table3() { TaskId = TaskId, TaskRefAircraftsID = RefAircraftsId });
}
}
}
如果该表包含外键关系,如何添加新值?
i try to write a kind of generic repository to add method.
Everything is ok to add but I have table which is related with two tables with FOREIGN KEY.But Not working because of foreign key
public class DomainRepository<TModel> : IDomainRepository<TModel> where TModel : class
{
private ObjectContext _context;
private IObjectSet<TModel> _objectSet;
public DomainRepository(ObjectContext context)
{
_context = context;
_objectSet = _context.CreateObjectSet<TModel>();
}
// do something...
public TModel Add<TModel>(TModel entity) where TModel : IEntityWithKey
{
EntityKey key = _context.CreateEntityKey(entity.GetType().Name, entity);
_context.AddObject(key.EntitySetName, entity);
_context.SaveChanges();
return entity;
}
// do something...
}
Calling Repository:
// insert-update-delete
public partial class AddtoTables
{
public table3 Add(int TaskId, int RefAircraftsId)
{
using (DomainRepository<table3> repTask = new DomainRepository<table3>(new TaskEntities()))
{
return repTask.Add<table3>(new table3() { TaskId = TaskId, TaskRefAircraftsID = RefAircraftsId });
}
}
}
How to add a new value if this table includes foreign key relation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该死的很难确定答案是否会有帮助,因为我不认为我清楚地理解你的界面,但为了将带有外键的对象添加到通用存储库,你将必须添加支持来添加带有外键的对象。我认为,如果您在签名中的某处有类似
paramsforeignkey[]
的内容,您将朝着支持具有外键的对象迈出一步。也许建议毫无价值,但我得到了有价值的建议。阅读 Martin Fowler 的《企业应用程序架构模式》,它将回答您所有的问题 - 我确信这一点。
Damn hard to be sure answer is going to be helpful cause I dont think I udnerstand your interface clearly but in order to add objects with foreign keys to generic repositary you will have to add support to add object with foreign keys. I think if you have something like
params ForeignKey[]
somewhere in signatures you will make a step towards having support objects with foreign keys. Probably advice is worthless, but I got a worthy advice.Read Patterns of Enterprise Application Architecture by Martin Fowler and it will answer all of your questions - in this I am sure.