Frann
Frann

Reputation: 51

To resolve this configure the foreign key properties explicitly on at least one of the relationships

i founded similar question but im stuck.

Im building an API rest, and i've got this error when im trying to add a migration

Both relationships between 'SessionSpeaker.Speaker' and 'Speaker' and between 'SessionSpeaker' and 'Speaker.SessionSpeakers' could use {'SpeakerId'} as the foreign key. To resolve this configure the foreign key properties explicitly on at least one of the relationships.

I have already see answer about it but it doesnt work ):

//Models:

public class Speaker : PlanificadorDTO.Speaker
{
    public virtual ICollection<SessionSpeaker> SessionSpeakers { get; set; } = new List<SessionSpeaker>();
}


public class SessionSpeaker
{
    public int SessionId { get; set; }

    public Session Session { get; set; }

    public int SpeakerId { get; set; }

    public Speaker Speaker { get; set; }
}

public class ApplicationDbContext : DbContext
{

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {

    }

    ////cuando se cree el modelo en Entity..
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PlanificadorAPI.Data.Attendee>()
            .HasIndex(a => a.UserName)
            .IsUnique();

        // Ignore the computed property
        modelBuilder.Entity<PlanificadorAPI.Data.Session>()
             .Ignore(s => s.Duration);

        // Many-to-many: Conference <-> Attendee
        modelBuilder.Entity<ConferenceAttendee>()
            .HasKey(ca => new { ca.ConferenceID, ca.AttendeeID });

        // Many-to-many: Session <-> Attendee
        modelBuilder.Entity<SessionAttendee>()
            .HasKey(ca => new { ca.SessionID, ca.AttendeeID });

        // Many-to-many: Speaker <-> Session
        modelBuilder.Entity<SessionSpeaker>()
           .HasKey(ss => new { ss.SessionId, ss.SpeakerId });

        // Many-to-many: Session <-> Tag
        modelBuilder.Entity<SessionTag>()
            .HasKey(st => new { st.SessionID, st.TagID });

    }

    public DbSet<Conference> Conferences { get; set; }

    public DbSet<Session> Sessions { get; set; }

    public DbSet<Track> Tracks { get; set; }

    public DbSet<Tag> Tags { get; set; }

    public DbSet<Speaker> Speakers { get; set; }

    public DbSet<Attendee> Attendees { get; set; }

}

DTO:

Speaker:

public class Speaker
{
    public int ID { get; set; }

    [Required]
    [StringLength(200)]
    public string Name { get; set; }

    [StringLength(4000)]
    public string Bio { get; set; }

    [StringLength(1000)]
    public virtual string WebSite { get; set; }

    public DateTime Nacimiento { get; set; }
}

Session:

public class Session
{
    public int ID { get; set; }

    [Required]
    public int ConferenceID { get; set; }

    [Required]
    [StringLength(200)]
    public string Title { get; set; }

    [StringLength(4000)]
    public virtual string Abstract { get; set; }

    public virtual DateTimeOffset? StartTime { get; set; }

    public virtual DateTimeOffset? EndTime { get; set; }

    public TimeSpan Duration => EndTime?.Subtract(StartTime ?? EndTime ?? DateTimeOffset.MinValue) ?? TimeSpan.Zero;

    public int? TrackId { get; set; }
}

Both relationships between 'SessionSpeaker.Speaker' and 'Speaker' and between 'SessionSpeaker' and 'Speaker.SessionSpeakers' could use {'SpeakerId'} as the foreign key. To resolve this configure the foreign key properties explicitly on at least one of the relationships.

Thanks for the help

Upvotes: 5

Views: 11748

Answers (2)

Stephen Himes
Stephen Himes

Reputation: 725

In Fluent API, define the ID and in my case, keep the data instead of cascade.

        modelBuilder.Entity<SessionSpeaker>()
            .HasOne(c => c.Speaker)
            .WithMany(c => c.Sessions)
            .HasForeignKey(c => c.SpeakerId)
            .OnDelete(DeleteBehavior.NoAction);

Upvotes: 1

Burak İŞLEYİCİ
Burak İŞLEYİCİ

Reputation: 109

Remove last changes, because you dont need this.

You need a ForeignKey.

[ForeignKey("SpeakerId")]
public Speaker Speaker { get; set; }

Add ForeignKey Attiribute this way.

Upvotes: 10

Related Questions