Adding multiple navigation properties of the same type in EF7

I have a model that looks like

public class Issue
{
    public Guid Id { get; set; }

    [Required]
    public User ReportedByUser { get; set; }

    public User ClosedByUser { get; set; }

    public Category Category { get; set; }
}

However, when I run ef migrations add <MigrationName>, I get the following error:

The navigation "ReportedByUser" for the entity type "WebProject.Models.Issue" is not added to the model or is ignored, or the target entityType is ignored.

I do not get this error when I have only 1 type navigation property Userin the model. How to do this with the model above?

+4
source share
3 answers

I was able to fix this by setting the following relationships in my DbContext.

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Issue>()
            .HasOne(i => i.ReportedByUser)
            .WithMany(u => u.Issues)
            .OnDelete(DeleteBehavior.Restrict);

        modelBuilder.Entity<Issue>()
                .HasOne(i => i.ClosedByUser)
                .WithMany(u => u.Issues)
                .OnDelete(DeleteBehavior.Restrict).IsRequired(false);

        base.OnModelCreating(modelBuilder);
    }

And create a model as shown below.

public class Issue
{
    public Guid Id { get; set; }   
    [Required]        
    public User ReportedByUser { get; set; }        
    public User ClosedByUser { get; set; }    
}
+1
source

, . , ReferencePropertyName + Id , ForeignKeyAttribute, EF .

public class Issue
{
        public Guid Id { get; set; }   

        public Guid ReportedByUserId { get; set; }
        public User ReportedByUser { get; set; }

        public Guid ClosedByUserId { get; set; }
        public User ClosedByUser { get; set; }    
}
+2

, , . . Weird.

0

All Articles