ModelBuilder icon indicating copy to clipboard operation
ModelBuilder copied to clipboard

Key property with private set isn't being discovered

Open Crimp opened this issue 4 years ago • 1 comments

When we have the private set for the Key property. The EF works well with it Read-only properties. And we have all BO with private/proteced Key set.

Reproduce steps

The next classes set :

    public class TestUser {
        public int ID { get; protected set; }
        public string FullName { get; set; }

        public virtual IList<TestRole> Roles { get; set; }
    }

    public class TestRole {
        public int ID { get; protected  set; }
        public string FullName { get; set; }

        public virtual IList<TestUser> Users { get; set; }
    }

Will work properly when we register both types and their keys manually:

ODataConventionModelBuilder builder = ...
Type typeToRegister = typeof(TestUser);
builder.AddEntityType(typeToRegister ).HasKey(typeToRegister.GetProperty("Id"))

typeToRegister = typeof(TestRole);
builder.AddEntityType(typeToRegister ).HasKey(typeToRegister.GetProperty("Id"))

But it's not convenient to register all types from types hierarchy manually. In general we want to register only top hierarchy objects and provide registration of other types to the system.

And when we register only one type from that example:

Type typeToRegister = typeof(TestUser);
builder.AddEntityType(typeToRegister ).HasKey(typeToRegister.GetProperty("Id"))

The TestRole type is resolved as Complex type and not the Entity type, because it can not discover it's Key property.

Expected result

The Key property with private/protected set is discovered and type is considered as the Entity type.

Actual result

The Model builder skip all properties with private/protected set.

Additional detail

The Microsoft.OData.ModelBuilder.ConventionsHelpers.GetAllProperties method doesn't return property with private set. After that the Microsoft.OData.ModelBuilder.EntityKeyConvention.GetKeyProperty cannot discover key property.

Crimp avatar Jan 19 '22 10:01 Crimp

Same bug here

SergioComputerx avatar Jul 07 '22 17:07 SergioComputerx