I am trying to create one-to-many relation between 2 classes and mainly I am trying to set a composite key between room number and campusid. I have tried using annotations and overriding the modelbuilder, but still nothing worked...
This is the exception that throws:
Unhandled Exception: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation: Room_Campus_Target_Room_Campus_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
Here are my models:
public class Room
{
public int Id { get; set; }
[StringLength(4, MinimumLength = 3)]
public string Number { get; set; }
public int CampusId { get; set; }
//[ForeignKey("Number, CampusId")]
public virtual Campus Campus { get; set; }
}
public class Campus
{
public Campus()
{
this.Rooms = new HashSet<Room>();
}
public int Id { get; set; }
[StringLength(4)]
public string Number { get; set; }
public virtual ICollection<Room> Rooms { get; set; }
}
Here is my modelbuilder attepmt with same exception..
modelBuilder.Entity<Room>()
.HasRequired(r => r.Campus)
.WithMany(c => c.Rooms)
.HasForeignKey(c => new { c.Number, c.CampusId });
Thanks in advance! :)