I got the following codes:
public class Trip
{
public int TripId { get; set; }
public int DriverId { get; set; }
public string StartingPoint { get; set; }
public string Destination { get; set; }
public DateTime TimeDepart { get; set; }
public int SeatAvailable { get; set; }
public virtual Carpooler Carpooler { get; set; }
public virtual ICollection<Passenger> Passengers { get; set; }
}
public class Driver
{
public int DriverId { get; set; }
public int TripId { get; set; }
public virtual Trip Trip { get; set; }
}
public class Passenger
{
public int PassengerId { get; set; }
public int TripId { get; set; }
public virtual Trip Trip { get; set; }
}
A model builder:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//one-to-one between Trip and Carpooler
modelBuilder.Entity<Trip>()
.HasRequired(x => x.Carpooler)
.WithRequiredDependent();
modelBuilder.Entity<Carpooler>()
.HasRequired(x => x.Trip)
.WithRequiredPrincipal();
base.OnModelCreating(modelBuilder);
//zero or one-to-many between Trip and Passenger
modelBuilder.Entity<Trip>()
.HasOptional(x => x.Passengers);
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
A initializer:
Database.SetInitializer<LiveGreenContext>(new CarpoolTripInitializer());
And i get the following error:
Multiplicity is not valid in Role ‘Trip_Driver_Target’ in relationship ‘Trip_Driver’. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.
Any solution for the problem? Thanks!
Configure the relationship between
TripandDriver. There is aDriverIdproperty inTripwhich is not configured. If you intend to use this as a navigational property you need to configure that too.Some of the configuration were incomplete in your
OnModelCreatingmethod and are corrected.