This is the method I am trying to invoke by Web API. I need to get user detail from active directory and I am trying to authenticate that for TFS Client. What I am trying to do in my app is, we are using ASPNetCore WEB API at the backend. As a result, it implicitly introduces an IdTempId property as the foreign key, and Id itself doesn’t appear as a property (it’s a navigation property instead).I am new to Web API to get the current user info. So it considers the Id property to be a navigation property to the ProductId entity. However, because ProductId is a “complex” type, EF Core assumes that it’s an entity, rather than a scalar value. One of the conventions is that a property named Id is assumed to be the key for the entity. However, if we just rely on the EF Core conventions, we’re going to run into a problem: entityType.GetProperties() does not return the Id property of Product! Let’s take a step back to understand why.īefore calling OnModelCreating to let the user customize the model, EF Core creates the model based on conventions. If we explicitly configure our entity primary keys and relations, this works fine. We use a cache to avoid redoing the same work multiple times. It dynamically generates the id => id.Value and value => new ProductId(value) expressions, using reflection and the Linq Expression API. The difficult part here is the CreateStronglyTypedIdConverter method. Return (ValueConverter)Activator.CreateInstance( Var converterType = typeof(ValueConverter) Var fromProviderExpression = Expression.Lambda( Var ctor = stronglyTypedIdType.GetConstructor( new ) Var valueParam = Expression.Parameter(valueType, "value") MakeGenericType(valueType, stronglyTypedIdType) Var toProviderExpression = Expression.Lambda(Įxpression.Property(stronglyTypedIdParam, "Value"), Var stronglyTypedIdParam = Expression.Parameter(stronglyTypedIdType, "id") MakeGenericType(stronglyTypedIdType, valueType) Private static ValueConverter CreateStronglyTypedIdConverter( Private static readonly ConcurrentDictionary StronglyTypedIdConverters = new()
![c how to get core id c how to get core id](https://i.ytimg.com/vi/9-cOd-xghYY/maxresdefault.jpg)
_ => CreateStronglyTypedIdConverter(property.ClrType, valueType)) Var converter = StronglyTypedIdConverters.GetOrAdd( If (StronglyTypedIdHelper.IsStronglyTypedId(property.ClrType, out var valueType)) Private static void AddStronglyTypedIdConversions(ModelBuilder modelBuilder)įoreach ( var entityType in ())įoreach ( var property in entityType.GetProperties()) This is done by the following method, to be called from OnModelCreating: We’re going to examine each property of each entity in the EF Core model, and if it’s a strongly-typed id, we’ll use reflection to generate the appropriate converter, and apply it to the property. Manually applying these conversions to each and every strongly-typed id in the model is going to get old pretty fast, right? So let’s fix that! Applying the conversion to all strongly-typed id properties There’s currently no way to say “apply this conversion for all properties of that type”, although it’s being considered for EF Core 6.0. Note that this has to be done for each property that is a strongly-typed id (whether it’s a primary key or foreign key). You can also wrap the two expressions in a ValueConverter object and reuse that object for multiple properties.
![c how to get core id c how to get core id](https://blog.kie.org/wp-content/uploads/2021/07/rule-rete.png)
HasConversion(id => id.Value, value => new ProductId( value)) The simplest way to do this is to specify expressions for converting the property to and from the database type: The way to tell EF Core how to do that is to to configure a value converter for properties that are strongly-typed ids. That column should be of a type compatible with the underlying type of the strongly-typed id, i.e.
![c how to get core id c how to get core id](https://i.stack.imgur.com/eLosP.png)
Definitely not what we want!Ī strongly-typed id is just wrapper for a single value, so it should be mapped to a single column in the same table as its declaring entity. Which means that if we don’t do anything, it will attempt to map ProductId to a ProductId table with a Value column.
![c how to get core id c how to get core id](http://2.bp.blogspot.com/-wSykmX95oS0/T8cf2LGYCYI/AAAAAAAAFQs/W0KIPuJgfq0/s1600/3dDoodle.jpg)
It just sees a custom type with no known conversion to a database type, so it assumes that it’s an entity. Out of the box, EF Core doesn’t know anything about our strongly-typed ids. Value conversion for a specific strongly-typed id Today, I’ll present another piece of the puzzle: how to make Entity Framework core handle strongly-typed ids correctly. I also explained how to make them work correctly with ASP.NET Core model binding and JSON serialization. Public record ProductId( int Value) : StronglyTypedId(Value)