You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Type String contain method StartsWith, but SKU as primitive type doesn't contain it.
In EF Core it leads me to use explicit converter in every place where I want to use some specific method.
My code:
usingMicrosoft.EntityFrameworkCore;usingMicrosoft.EntityFrameworkCore.Storage.ValueConversion;namespacePrimitiveTypes;publicclassProgram{publicstaticasyncTaskMain(paramsstring[]args){MyContextcontext=new();context.Database.EnsureDeleted();context.Database.EnsureCreated();varsku=newSKU("myValue");//example 1//it worksProduct?result1=awaitcontext.Products.FirstOrDefaultAsync(x =>x.SKU==sku);//example 2//it also worksProduct?result2=awaitcontext.Products.FirstOrDefaultAsync(x =>((string)x.SKU).StartsWith("R"));//example 3//throw an ExceptionProduct?result3=awaitcontext.Products.FirstOrDefaultAsync(x =>x.SKU.Value.StartsWith("R"));}}publicclassProduct{publicintId{get;set;}publicSKUSKU{get;set;}}publicreadonlystructSKU{publicSKU(stringvalue){Value=value;}publicstringValue{get;}publicboolEquals(SKUother)=>this.Value.Equals(other.Value);publicoverrideboolEquals(object?obj){if(objisnull)returnfalse;returnobjisSKUother&&Equals(other);}publicoverrideintGetHashCode()=>Value.GetHashCode();publicoverridestringToString()=>Value.ToString();publicstaticbooloperator==(SKUa,SKUb)=>a.Equals(b);publicstaticbooloperator!=(SKUa,SKUb)=>!(a==b);publicintCompareTo(SKUother)=>Value.CompareTo(other.Value);publicstaticexplicitoperatorstring(SKUsku)=>sku.Value;}publicclassSKUConverter:ValueConverter<SKU,string>{publicSKUConverter():base(x =>x.Value, x =>newSKU(x)){}}publicclassMyContext:DbContext{publicDbSet<Product>Products{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder){optionsBuilder.UseNpgsql(@"Host=localhost;Port=7435;Database=testdb;Username=admin;Password=testpass").LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();}protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<Product>(builder =>{builder.ToTable("Products");builder.Property(p =>p.Id).HasColumnName("Id");builder.Property(p =>p.SKU).HasConversion<SKUConverter>();});}}
I want to use expressions as in the third example. But it doesn't work and I know about it. Right now the solution is to use it like in the 2nd example.
The main problem is lack of internal type's methods using primitive types. Using Value is a lot more convenient way than using explicit operator.
Describe the solution you'd like
Primitive types is constructed by ValueConverter. Constructor of ValueConverter contains a LambdaExpression. The LambdaExpression can be deconstructed to expression tree where primitive type looks like x.Value or similar.
And the expression from database query also contains the same syntax in my third example.
Сan you consider adding this translation of LINQ expression?
The text was updated successfully, but these errors were encountered:
What problem are you trying to solve?
I use primitive types in my project. But primitive types doesn't contains all methods which exists in internal type.
Type String contain method StartsWith, but SKU as primitive type doesn't contain it.
In EF Core it leads me to use explicit converter in every place where I want to use some specific method.
My code:
I want to use expressions as in the third example. But it doesn't work and I know about it. Right now the solution is to use it like in the 2nd example.
The main problem is lack of internal type's methods using primitive types. Using Value is a lot more convenient way than using explicit operator.
Describe the solution you'd like
Primitive types is constructed by ValueConverter. Constructor of ValueConverter contains a LambdaExpression. The LambdaExpression can be deconstructed to expression tree where primitive type looks like x.Value or similar.
And the expression from database query also contains the same syntax in my third example.
Сan you consider adding this translation of LINQ expression?
The text was updated successfully, but these errors were encountered: