question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InsertAndGetId return wrong Id when we have byte as primary key.

See original GitHub issue

When we have byte as primary key it return highest number that byte can hold as a primary key for the first time after that it decreases the number by 1.

so first time it returns 255, second time 244 and so on …

When I’m stopping the app and again running the app it again return id as 255 first time and decreases by one each time.

but in the database it does save the id starting from 1 ,2 ,3 , 4

Entity

 public  class TestEntity :Entity<byte>
    {
    }

DbContext

  public virtual DbSet<TestEntity.TestEntity> TestEntities { get; set; }
        public TestProjectDbContext(DbContextOptions<TestProjectDbContext> options)
            : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder
                .Entity<TestEntity.TestEntity>(
                     entity =>
                     {
                         entity.Property(e => e.Id).ValueGeneratedOnAdd();
                     });

        }

Application Service

  public class TestAppService : TestProjectAppServiceBase, ITestAppService
    {
        private readonly IRepository<TestEntity, byte> _repository;

        public TestAppService(IRepository<TestEntity,byte> repository)
        {
            _repository = repository;
        } 
        public async Task<byte> InsertorUpdateAndGetIdAsync(TestEntity entity)
        {
            var res =await  _repository.InsertAndGetIdAsync(entity);
            return res;
        }
    }

This is the issue with all the inserts that returns id (InsertAndGetIdAsync,InsertAndGetId , InsertOrUpdateAndGetId and InsertOrUpdateAndGetIdAsync)

  • Abp package version : 4.1.0
  • base framework: .Net Core.
  • SQL server

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
hikalkancommented, Jan 25, 2019

Yes, this will help. When it sets to a value like 255, ABP thinks that it’s already saved and this is a real id value and does not call Context.SaveChangesAsync().

However, I suggest you to don’t use byte as PK. Microsoft’s implementation is not good. If you ever will have 255 different values, why do you care about the PK size, just use int 😃

0reactions
iAmBipinPaulcommented, Jan 26, 2019

Okay , will use int for Pk. Thank you 👍 !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using byte as the primary key datatype
I am using Entity Framework code-first. I have a table the will not exceed 100 rows and I would like to use the...
Read more >
c# - Using a GUID as a Primary Key
Say I had a database containing millions and millions of rows with a GUID as the Primary Key. Millions and millions, you are...
Read more >
byte[] as primary key - Developer Community - Oracle Forums
I find it strange that the DPL, which is built on top of a database using byte arrays as primary keys, does not...
Read more >
gdb
InsertAndGetId performs action Insert and returns the last insert id that automatically generated. func (*Core) InsertIgnore ¶. func (c *Core) ...
Read more >
UUID or GUID as Primary Keys? Be Careful! | by Tom Harrison
It's just a few bytes, right? Primary keys get around in normalized databases. If you have a well normalized database, as we do...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found