Is it somehow possible to define navigation properties in EFCore with private or protected access level to make this kind of code work:
class Model {
public int Id { get; set; }
virtual protected ICollection<ChildModel> childs { get; set; }
}
A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data.
An Entity can include two types of properties: Scalar Properties and Navigation Properties. Scalar Property: The type of primitive property is called scalar properties. Each scalar property maps to a column in the database table which stores the real data.
Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.
You have two options, using type/string inside the model builder.
modelBuilder.Entity<Model>(c =>
c.HasMany(typeof(Model), "childs")
.WithOne("parent")
.HasForeignKey("elementID");
);
Not 100% sure it works with private properties, but it should.
modelBuilder.Entity<Model>(c =>
c.HasMany(typeof(Model), nameof(Model.childs)
.WithOne(nameof(Child.parent))
.HasForeignKey("id");
);
Or use a backing field.
var elementMetadata = Entity<Model>().Metadata.FindNavigation(nameof(Model.childs));
elementMetadata.SetField("_childs");
elementMetadata.SetPropertyAccessMode(PropertyAccessMode.Field);
Alternatively try that with a property
var elementMetadata = Entity<Model>().Metadata.FindNavigation(nameof(Model.childs));
elementMetadata.SetPropertyAccessMode(PropertyAccessMode.Property);
Be aware, as of EF Core 1.1, there is a catch: The metadata modification must be done last, after all other .HasOne/.HasMany
configuration, otherwise it will override the metadata. See Re-building relationships can cause annotations to be lost.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With