I have an entity User
that has two properties CreatedBy
and UpdatedBy
both referencing User
. By default, EF assumes that these two as a one to one relation to each other. I get the following error message:
Message: System.InvalidOperationException : The child/dependent side could not be determined for the one-to-one relationship that was detected between 'User.CreatedBy' and 'User.UpdatedBy'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.
Currently, I have a class like this:
public class User
{
public int Id { get; set; }
public int? CreatedById { get; set; }
public User CreatedBy { get; set; }
public int? UpdatedById { get; set; }
public User UpdatedBy { get; set; }
}
Basically, here is what I am trying:
CreatedBy
and any number of users can have the same UpdatedBy
. How can I ask EF to just ignore the navigation property? The main reason I have CreatedBy
is so I can use Include(u => u.CreatedBy)
later on. I know using IEnumerable<User> AllCreatedUsers
property would solve this but I did not want to create an IEnumerable
for each in my entity. Is there any way to do this with fluent API?
Here is what I tried:
modelBuilder.Entity<User>()
.Property<IEnumerable<User>>("AllCreatedUsers");
modelBuilder.Entity<User>().HasOne(u => u.CreatedBy)
.WithMany(u => EF.Property<IEnumerable<User>>(u, "AllCreatedUsers"));
You need to configure two Single Navigation Property relationships by using parameterless WithMany
method overload:
modelBuilder.Entity<User>()
.HasOne(u => u.CreatedBy)
.WithMany();
modelBuilder.Entity<User>()
.HasOne(u => u.UpdatedBy)
.WithMany();
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