Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Postgresexception 428C9 cannot insert a non default value into column id #3422

Closed
FatmaSedaOZYURT opened this issue Dec 26, 2024 · 5 comments

Comments

@FatmaSedaOZYURT
Copy link

FatmaSedaOZYURT commented Dec 26, 2024

Hi,
I develope my project on VisualStudio with c# and postgresql by using EF codefirst.
When data is created on table, sometimes I got this error: Postgresexception 428C9 cannot insert a non default value into column id.

My class is :
public class MyClass { public long Id; public string Name { get; set; } public string LastName { get; set; } }

And my builder is :
model.Entity<MyClass>().Property(b => b.Id).UseIdentityAlwaysColumn();

And later I done migration section. My table is created.

Without seting an Id, that is, with the default value, I filled other values.
Like this:
{ "Name" : "test", "LastName" : "test1" }
But I have 428C9 error.
What can I do this problem?

@FatmaSedaOZYURT
Copy link
Author

FatmaSedaOZYURT commented Dec 27, 2024

Hi,
I tried this model.
model.Entity().Property(b => b.Id).ValueGeneratedOnAdd();

However, I keep getting error 428C9.
For a while, it worked but later I start to take error

@roji
Copy link
Member

roji commented Dec 28, 2024

@FatmaSedaOZYURT can you please submit a minimal, runnable code sample that shows the problem?

@FatmaSedaOZYURT
Copy link
Author

FatmaSedaOZYURT commented Dec 30, 2024

Hi @roji,
That is my code:

public class MyClass 
{ 
	public long Id; 
	public string Name { get; set; } 
	public string LastName { get; set; }
}
public async Task<MyClassDto> Create(MyClassDto entityDto)
{
    var entity = _mapper.Map<MyClass>(entityDto);
    entity.DateCreated = DateTime.UtcNow;
    
    await DbContext.Set<MyClass>().AddAsync(entity);
    await DbContext.SaveChangesAsync();
    
    return _mapper.Map<MyClassDto>(entity);
}

@roji
Copy link
Member

roji commented Dec 30, 2024

@FatmaSedaOZYURT that's not a minimal, runnable code sample - it's a fragment that's missing important information such as your model configuration. Please try to put together a minimal console program that triggers the error, and post that here.

@FatmaSedaOZYURT
Copy link
Author

FatmaSedaOZYURT commented Jan 3, 2025

Hi, thank you for your interest @roji .
I soluted this problem.
When I look in DB log file, I realized to take error because of different another table. As a result of an incorrect create operation on a different table, most likely the DB locked for this context.

I wonder when the locked context will be opened. 👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@roji @FatmaSedaOZYURT and others