我的问题是,我是否在类和实体配置中做了正确的关系
我应该在哪里做1: n的关系?在船班还是InfoAppointment班?
我是在InfoAppointment做的,但我很怀疑。
InfoWeather,它将始终与InfoAppointment一起注册。InfoWeather不在其他地方注册,因此对于每个InfoAppointment条目记录,我可以拥有一个infoWeather,而且infoWeather也可以是可选的。
我也对0或1的配置有疑问,对吗?
我的类InfoAppointment只能有一个Boat
我的Boat类可以在多个InfoAppointment中。
我的类InfoAppointment可以有零个或一个InfoWeather
我的类InfoWeather可以有一个或零个InfoAppointment
public class InfoAppointment : Entity
{
public Guid Boat_Id { get; set; }
public virtual Boat Boat { get; set; }
public virtual InfoWeather infoWeather { get; set; }
}
public class InfoWeather : Entity
{
public virtual InfoAppointment InfoAppointment { get; set; }
}
public class Boat : Entity
{
public virtual ICollection<InfoAppointment> InfoAppointment { get; set; }
}
public class InfoAppointmentConfig : EntityTypeConfiguration<InfoAppointment>
{
HasRequired(c => c.Boat)
.WithMany(c => c.InfoAppointment )
.HasForeignKey(c => c.Boat_Id);
HasOptional(c => c.InfoWeather )
.WithOptionalDependent(c => c.InfoAppointment );
}
public class BoatConfig : EntityTypeConfiguration<Boat>
{
}
public class InfoWeatherConfig : EntityTypeConfiguration<InfoWeather >
{
}发布于 2019-01-11 15:09:47
您的要求如下:
InfoAppointment只能有一个BoatBoat可以在多个InfoAppointment中。InfoAppointment可以有零个或一个InfoWeatherInfoWeather可以有一个或零个InfoAppointment然后将InfoAppointment、Boat和InfoWeather之间的关系写成如下:
public class InfoAppointment : Entity
{
[Key]
public Guid InfoAppointmentId { get; set; }
[ForeignKey("Boat")]
public Guid BoatId {get; set;}
// other properties
public virtual Boat Boat { get; set; }
public virtual InfoWeather infoWeather { get; set; }
}
public class Boat : Entity
{
public Guid BoatId {get; set;}
// other properties
public virtual List<InfoAppointment> InfoAppointments { get; set; }
}
public class InfoWeather : Entity
{
[Key,ForeignKey("InfoAppointment")]
public Guid InfoAppointmentId { get; set; }
// other properties
public virtual InfoAppointment InfoAppointment { get; set; }
}直到第三项要求,一切都好。你的第四个要求
InfoWeather可以有一个或零个InfoAppointment一定要确保你真的想要这个。然后,您必须按以下方式更新InfoAppointment和InfoWeather实体:
public class InfoAppointment : Entity
{
public Guid InfoAppointmentId { get; set; }
[ForeignKey("Boat")]
public Guid BoatId {get; set;}
[ForeignKey("InfoWeather")]
public Guid InfoWeatherId {get; set;}
// other properties
public virtual Boat Boat { get; set; }
public virtual InfoWeather InfoWeather { get; set; }
}
public class InfoWeather : Entity
{
[Key]
public Guid InfoWeatherId { get; set; }
[ForeignKey("InfoAppointment")]
public Guid InfoAppointmentId { get; set; }
// other properties
public virtual InfoAppointment InfoAppointment { get; set; }
}现在必须使用FluentAPI指定主体实体如下:
modelBuilder.Entity<InfoAppointment>()
.HasOptional(x => x.InfoWeather)
.WithOptionalPrincipal(x => x.InfoAppointment)
.Map(a => a.MapKey("InfoWeatherId"));希望这对你有用!
https://stackoverflow.com/questions/54148387
复制相似问题