InsertAndGetId return wrong Id when we have byte as primary key.
See original GitHub issueWhen 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:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 😃
Okay , will use int for Pk. Thank you 👍 !