Howw to add new value with generic Repository if there are foreign keys (EF-4)?

Posted by Phsika on Stack Overflow See other posts from Stack Overflow or by Phsika
Published on 2010-12-23T12:27:23Z Indexed on 2010/12/23 12:54 UTC
Read the original article Hit count: 230

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

alt text



   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>();

        }


//do something.....
public TModel Add<TModel>(TModel entity) where TModel : IEntityWithKey
        {
            EntityKey key;
            object originalItem;
            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

alt text

© Stack Overflow or respective owner

Related posts about c#

Related posts about visual-studio-2010