-
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
Support value generation with converters #11597
Comments
Consider the Oracle case in #11559 when looking into this. |
Note: when working on this, also consider #11970 |
Is anyone already working on this and is there an ETA? If not, would you accept PR's for this? |
@twsI It's in the 3.0 milestone, which means it is tentatively planned for 3.0. I don't believe anyone is working on it right now. We would consider PRs, but it would be helpful if you could provide some details of what you plan to do before submitting a PR, since it's not clear what the behavior here should be or how it should be implemented. |
Part of #11597 This change takes the ValueComparer defined for the principal key and uses it for the foreign key, but also accommodating for nulls appropriately. As part of this, we started getting some more complex expressions in value comparers used in the in-memory database. These expressions became part of the query, which then meant they needed to be translated. Therefore, this logic has been changed to call the value comparer as a method when using the in-memory database, and this method is then detected. This incidentally fixes #27495, which was also a case of a value comparer expression that could not be translated, and any other case where a value comparer could not be translated in in-memory queries.
Providers may need to be updated to support this. Fixes #11597
Providers may need to be updated to support this. Fixes #11597
Have anyone got this to work on a strongly typed Id using Guids? I can get it to work on Integer types, but not using Guids in the record struct.
If I apply both the following errors is produced when building the migration:
If I comment out the Guid version, the migration builds and the following is produced:
|
I think you shouldn't use .Useidentity with a guid. Also where do you expect the guid to be generated? Db or from the application? |
I can be I misunderstand the documentation, but it says Guids can be auto generated by the provider. If this happens on the client (by the provider) or in the Db, doesn't make a difference to me. Primary keys |
@lars-lindstrom You need to use modelBuilder
.Entity<Blog>()
.Property(entity => entity.Id)
.ValueGeneratedOnAdd()
.HasConversion<StronglyTypedGuidIdValueConverter>(); Runnable: using (var context = new SomeDbContext())
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Add(new Blog { Name = "X" });
context.SaveChanges();
}
using (var context = new SomeDbContext())
{
var blogs = context.Blogs.ToList();
}
public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=AllTogetherNow")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
public DbSet<Blog> Blogs => Set<Blog>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Blog>()
.Property(entity => entity.Id)
.ValueGeneratedOnAdd()
.HasConversion<StronglyTypedGuidIdValueConverter>();
}
}
public class StronglyTypedGuidIdValueConverter : ValueConverter<StronglyTypedGuidId, Guid>
{
public StronglyTypedGuidIdValueConverter()
: base(v => v.Value, v => new StronglyTypedGuidId(v))
{
}
}
public readonly record struct StronglyTypedGuidId
{
public StronglyTypedGuidId(Guid id)
{
Value = id;
}
public Guid Value { get; }
public static StronglyTypedGuidId Create(Guid entityId) => new(entityId);
}
public class Blog
{
public StronglyTypedGuidId Id { get; set; }
public string? Name { get; set; }
} |
@ajcvickers Thx. Any way, huge thank you. And great job you and rest the team are doing on EF Core. |
@lars-lindstrom By default they are generated on the client. This can be changed to server generating using |
Current behavior is to throw by default if a converter is associated with a property that will use value generation
However, a value generator to use can be specified:
A client-side generator is added to the Guid converters so that Guid key scenarios still work. We should find similar "safe" cases and make those work as well.
Original PR: #11479
The text was updated successfully, but these errors were encountered: