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

问题描述 投票:0回答:1

我提出了类似的问题,但我坚持了。

我正在构建API其余部分,而我在尝试添加迁移时遇到此错误

'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'}作为外键。要解决此问题,请至少在一种关系上显式配置外键属性。

感谢您的帮助

c# entity-framework asp.net-core .net-core
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.