-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Auto incrementing primary key with value converter does not work #13814
Comments
Duplicate of #11597. @codenesium For workarounds, you could try #11970 (comment) |
@ajcvickers I looked at 11970 and it almost works. Do you see anything I've done here that looks wrong? I'm getting a typecast exception. I'm converting the values to strings out of desperation. I can't tell from which part the exception comes from.
|
@codenesium I was able to make this work: public class BloggingContext : DbContext
{
private static readonly LoggerFactory Logger
= new LoggerFactory(new[] { new ConsoleLoggerProvider((_, __) => true, true) });
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseLoggerFactory(Logger)
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var converter = new ValueConverter<int, decimal>(
v => v,
v => (int)v,
new ConverterMappingHints(valueGeneratorFactory: (p, t) => new TemporaryIntValueGenerator()));
modelBuilder.Entity<Address>()
.Property("Id")
.ValueGeneratedOnAdd()
.UseSqlServerIdentityColumn()
.HasConversion(converter);
}
}
public class Address
{
[Column("add_id", TypeName = "decimal")]
public int Id { get; private set; }
}
public class Program
{
public static void Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.AddRange(new Address(), new Address());
context.SaveChanges();
}
using (var context = new BloggingContext())
{
var addresses = context.Set<Address>().ToList();
}
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a primary key in my SQL database declared as
I'm getting an error that auto incrementing a key doesn't work with a value converter. I want the id in my code to be an int because that's really what is it. I know behind the scenes you're adding a casting converter to map the decimal in the database to the int in the model. Some dumb dumb back in the day declared our primary keys as numeric instead of int. Is there a work around for this short of explicitly going to the database to get top 1 id and adding one to it?
Relevant code in entity
Relevant code in OnModelCreating
I've also tried not explicitly setting the type to decimal but then I just get a decimal to int conversion issue.
Steps to reproduce
Repo to reproduce
https://github.com/codenesium/tmp
Further technical details
EfCore.SqlServer 2.1.3
Database Provider:
Operating system:
Windows
The text was updated successfully, but these errors were encountered: