Note that there are some explanatory texts on larger screens.

plurals
  1. POBlToolkit insert data failure in BaseRepository class
    primarykey
    data
    text
    <p>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 and BaseEntity classes:</p> <pre><code>public class BaseRepository&lt;TEntity, T&gt; : IRepository&lt;TEntity, T&gt; where TEntity : BaseEntity&lt;T&gt; { private DbManager _context; private Table&lt;TEntity&gt; Table { get { return _context.GetTable&lt;TEntity&gt;(); } } public BaseRepository(DbManager context) { _context = context; } //... public TEntity Add(TEntity entity) { //... return entity; } public TEntity Edit(TEntity entity) { _context.Update(entity); return entity; } //... } public class BaseEntity&lt;T&gt; { [PrimaryKey] public T Id { get; set; } } </code></pre> <p>I tried three ways for Add operation to make it work. First two ways gave errors.</p> <p><strong>First way(Doesn't work):</strong></p> <pre><code>public TEntity Add(TEntity entity) { _context.Insert(entity); return entity; } </code></pre> <p><em>Error Message:</em> <code>Cannot insert explicit value for identity column in table '...' when IDENTITY_INSERT is set to OFF.</code></p> <p>--</p> <p><strong>Second way(Doesn't work):</strong></p> <pre><code>public TEntity Add(TEntity entity) { Table.Insert(() =&gt; entity); return entity; } </code></pre> <p><em>Error Message:</em> <code>Operation is not valid due to the current state of the object.</code></p> <p>--</p> <p><strong>Third way(Working):</strong></p> <pre><code>public TEntity Add(TEntity entity) { var l = new List&lt;TEntity&gt; { entity }; _context.InsertBatch(l); return entity; } </code></pre> <p>--</p> <p><del>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?</del></p> <p>I tried advice of @Mladen Macanović and I added <code>Identity</code> attribute to primary key in base BaseEntity class, <em>then errors shown above gone for entities having int type primary key</em>. </p> <p><strong>Errors shown above gone for entities having int type of primary key:</strong></p> <pre><code>public class BaseEntity&lt;T&gt; { [PrimaryKey Identity] public T Id { get; set; } } </code></pre> <p>But, this is not a full solution because, <strong>some of my entities have primary key in type of Guid</strong>, so adding <code>Identity</code> attribute to them gives another error.</p> <p><strong>InsertBatch method works without using Identity attribute.</strong> So, you can add data without using Identity attribute in BaseEntity class for Identity column. What is the difference of insertbatch method? How can I resolve errors shown above without using <code>InsertBatch</code> method?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload