要解决此问题,请至少在一种关系上显式配置外键属性

我提出了类似的问题,但我被困住了。

我正在构建一个API rest,我在尝试添加迁移时遇到了这个错误

“ SessionSpeaker.Speaker”和“ Speaker”之间以及“ SessionSpeaker”和“ Speaker.SessionSpeakers”之间的关系都可以使用{'SpeakerId'}作为外键。要解决此问题,请至少在一种关系上显式配置外键属性。

我已经看到有关它的答案,但它不起作用):

//型号:

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:

扬声器:

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; }
}

会话:

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; }
}

“ SessionSpeaker.Speaker”和“ Speaker”之间以及“ SessionSpeaker”和“ Speaker.SessionSpeakers”之间的关系都可以使用{'SpeakerId'}作为外键。要解决此问题,请至少在一种关系上显式配置外键属性。

感谢您的帮助

zqp19880915 回答:要解决此问题,请至少在一种关系上显式配置外键属性

嗨,我做了一些更改,现在出现以下错误。

“扬声器”的鉴别值是“扬声器”,与“扬声器”相同。层次结构中的每种具体实体类型都需要具有唯一的鉴别符值。

,

删除最后的更改,因为您不需要此。

您需要一个ForeignKey。

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

以这种方式添加ForeignKey属性。

本文链接:https://www.f2er.com/3150680.html

大家都在问