Open Closed

Database querying on Extension Properties #3824


User avatar
0
naeem76 created

I extended the existing IdentityUser with the extension system, and also had it mapped to a different column using ef core mapping, which is working fine for the UI side as well

 ObjectExtensionManager.Instance.Modules()
            .ConfigureIdentity(identity =>
            {
                identity.ConfigureUser(user =>
                {
                    user.AddOrUpdateProperty<Guid?>(
                        "ClassroomId",
                        property =>
                        {
                            //property.Attributes.Add(new RequiredAttribute());

                            //validation rules
                            property.DisplayName = new FixedLocalizableString("Classroom");
                            property.UI.Lookup.Url = "/api/app/classrooms";
                            property.UI.Lookup.DisplayPropertyName = "name";
                            property.UI.Lookup.FilterParamName = "filterText";
                        }
                    );
                });
            });
ObjectExtensionManager.Instance
        .MapEfCoreProperty<IdentityUser, Guid?>(
                    "ClassroomId",
                    (entityBuilder, propertyBuilder) =>
                    {
                        entityBuilder
                            .HasOne(typeof(Classroom))
                            .WithMany()
                            .HasForeignKey("ClassroomId");
                    }
                );

How to query on that property when querying from database directly and not using client side evaluation?

The doc mentions 'Another approach can be creating your own entity mapped to the same database table (or collection for a MongoDB database).`

Tried by making a class AppUser that inherits from IdentityUser mapped to the same table 'AbpUsers' however that makes a discriminator column, and queries with the new class dont bring out all records, which I think is expected from EF Core TablePerHierarchy.

What's the ideal solution for querying something like this?

 var users = await Users.Where(x=> x.GetClassroomId() == classId).ToListAsync(); 
public static class IdentityUserExtensions
    {
        private const string ClassroomIdPropertyName = "ClassroomId";

        public static void SetClassroomId(this IdentityUser user, Guid? classroomId)
        {
            user.SetProperty(ClassroomIdPropertyName, classroomId);
        }
        public static Guid? GetClassroomId(this IdentityUser user)
        {
            return user.GetProperty&lt;Guid?&gt;(ClassroomIdPropertyName);
        }
    }

Also I would rather not go for a new New Entity with Its Own Database Table, and using local bus to keep them synchronized, unless its the last option


2 Answer(s)
  • User Avatar
    0
    manuel42 created

    Use the following code snippet instead of x.GetClassroomId()

    EF.Property<Guid>(x, "ClassroomId")
    

    More information about this topic you can get here https://www.learnentityframeworkcore.com/model/shadow-properties

  • User Avatar
    0
    naeem76 created

    Thank you!

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11