Reputation: 1655
I tries to figured it out why this is not wokring and getting me:
System.InvalidOperationException: The relationship from 'Meeting.MeetingType' to 'MeetingType.Meeting' with foreign key properties {'MeetingId' : string} cannot target the primary key {'MeetingTypeId' : int} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.
I want to have that Meeting
can have one MeetingType
but MeetingTypes
can have many meetings
Meeting
public class Meeting {
public string MeetingId { get; set; }
public DateTime? MeetingDate { get; set; }
public int MeetingTypeId { get; set; }
public MeetingType MeetingType { get; set; }
public Int64 StudyId{ get; set; }
public Study Study{ get; set; }
}
Meeting Type
public class MeetingType {
public int MeetingTypeId { get; set; }
public string MeetingTypeName { get; set; }
public Meeting Meeting {get; set;}
}
Context
modelBuilder.Entity<MeetingType> (entity => {
entity.ToTable ("MeetingType", "tabel")
.HasOne (x => x.Meeting)
.WithOne (x => x.MeetingType)
.HasForeignKey<Meeting> (x => x.MeetingId);
});
modelBuilder.Entity<Meeting> (entity => {
entity.ToTable ("Meeting", "tabel")
.HasOne (x => x.Study)
.WithMany (x => x.Meeting)
.HasForeignKey (x => x.StudyId);
});
Upvotes: 0
Views: 85
Reputation: 1
Try this.
Change the foreign key from MeetingId to MeetingTypeId
`modelBuilder.Entity<MeetingType>(entity => {
entity.ToTable("MeetingType", "tabel")
.HasOne(x => x.Meeting)
.WithOne(x => x.MeetingType)
.HasForeignKey<Meeting>(x => x.MeetingTypeId);
});`
Upvotes: 0
Reputation: 43959
Change your class:
public class MeetingType {
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Meeting> Meetings {get; set;}
}
and db context ( if you use EF Net5 you don't need to use it):
modelBuilder.Entity<Meeting>(entity =>
{
entity.HasOne(d => d.MeetingType)
.WithMany(p => p.Meetings)
.HasForeignKey(d => d.MeetingTypeId)
.OnDelete(DeleteBehavior.ClientSetNull);
});
Upvotes: 3