Key property with private set isn't being discovered
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 22
- Forks
- 24
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Microsoft.OData.ModelBuilder.ConventionsHelpers.GetAllProperties and Microsoft.OData.ModelBuilder.EntityKeyConvention.GetKeyProperty, using the TestUser and TestRole reproduction in the issue. Trace why the protected key setter is excluded and verify that registering TestUser causes TestRole to be recognized as an entity with its key discovered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100