BlToolkit insert data failure in BaseRepository class

79 views
Skip to first unread message

seref...@gmail.com

unread,
Dec 11, 2013, 7:11:57 AM12/11/13
to blto...@googlegroups.com
I asked this post as a question in stackoverflow as follows:

I'm using BaseRepository in asp .Net MVC project. Edit operation works but in Add operation, I should make a trick to make it work. In detail, my base repository class:

public class BaseRepository<TEntity, T> : IRepository<TEntity, T> where TEntity : BaseEntity<T>
{
    private DbManager _context;

    private Table<TEntity> Table
    {
        get { return _context.GetTable<TEntity>(); }
    }

    public BaseRepository(DbManager context)
    {
        _context = context;
    }

    //...

    public TEntity Add(TEntity entity)
    {
        //...

        return entity;
    }

    public TEntity Edit(TEntity entity)
    {
        _context.Update(entity);

        return entity;
    }

    //...
}

I tried three ways for Add operation to make it work. First two ways gave errors.

First way(Doesn't work):

public TEntity Add(TEntity entity)
    {
        _context.Insert(entity);            

        return entity;
    }

Error Message: Cannot insert explicit value for identity column in table '...' when IDENTITY_INSERT is set to OFF.

--

Second way(Doesn't work):

public TEntity Add(TEntity entity)
    {
        Table.Insert(() => entity);          

        return entity;
    }

Error Message: Operation is not valid due to the current state of the object.

--

Third way(Working):

public TEntity Add(TEntity entity)
    {
        var l = new List<TEntity> { entity };
        _context.InsertBatch(l);            

        return entity;
    }

--

Edit operation works without error, but for Add operation I need to make some trick. What is the problem with normal Add operation and, is there a way to make it work?

Reply all
Reply to author
Forward
0 new messages