How to add new value with generic Repository if there are foreign keys (EF-4)?
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<table开发者_如何学Python3>(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?
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.
精彩评论