I have UserEntity:
public record UserEntity
{
public int Id { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string? LastName { get; set; }
public string Email { get; set; }
public string PasswordHash { get; set; }
public Roles[] Roles { get; set; }
public SchoolEntity? School { get; set; }
public int? SchoolId { get; set; }
public DateTime DateOfCreating { get; set; }
public IEnumerable<ExamOptionEntity>? ExamOptionsCreated { get; set; }
}
And SchoolEntity:
public record SchoolEntity
{
public int Id { get; set; }
public UserEntity Author { get; set; }
public int AuthorId { get; set; }
public IEnumerable<UserEntity> Students { get; set; }
public string Link { get; set; }
public PageEntity Page { get; set; }
public int PageId { get; set; }
public Subjects Subject { get; set; }
public DateTime DateOfCreating { get; set; }
}
I need to create relationship, because School has Author and Students.
Here are fluent configuration methods:
public void Configure(EntityTypeBuilder<UserEntity> builder)
{
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedOnAdd();
builder.HasOne(x => x.School)
.WithOne(x => x.Author)
.HasPrincipalKey<UserEntity>(x => x.Id)
.HasForeignKey<SchoolEntity>(x => x.AuthorId);
}
And
public void Configure(EntityTypeBuilder<SchoolEntity> builder)
{
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).ValueGeneratedOnAdd();
builder.HasMany(x => x.Students)
.WithOne(x => x.School)
.HasForeignKey(x => x.SchoolId)
.HasPrincipalKey(x => x.Id);
}
Finally I cached exception: "Cannot create a relationship between \u0027SchoolEntity.Students\u0027 and \u0027UserEntity.School\u0027 because a relationship already exists between \u0027UserEntity.School\u0027 and \u0027SchoolEntity.Author\u0027. Navigations can only participate in a single relationship. If you want to override an existing relationship call \u0027Ignore\u0027 on the navigation \u0027UserEntity.School\u0027 first in \u0027OnModelCreating\u0027."
when I ran the application.
What could be the problem?