实体配置:如何正确配置 类 和实体配置

Entity Configurantion: How to properly configure classes and the Entity configuration

我的问题是我在 classes 和实体配置中是否建立了正确的关系

1:n关系应该在哪里做?在船 class 或 InfoAppointment class?
我在 InfoAppointment 中做了,但我有疑问。

InfoWeather 将始终与 InfoAppointment 一起注册。 InfoWeather 没有在其他地方注册,所以对于每个 InfoAppointment 条目记录,我可以有一个 infoWeather,此外 infoWeather 也可以是可选的

我也对0或1的配置有疑问,对吧?

我的ClassInfoAppointment只能有一个Boat

我的BoatClass可以在多个InfoAppointment.

我的ClassInfoAppointment可以有零个或一个InfoWeather

我的ClassInfoWeather可以有一个或零个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 >
{
}

您的要求如下:

  1. My Class InfoAppointment can have only one Boat

  2. My Class Boat can be in multiple InfoAppointment.

  3. My Class InfoAppointment can have zero or one InfoWeather

  4. My Class InfoWeather can have one or zero InfoAppointment

然后写出InfoAppointmentBoatInfoWeather的关系如下:

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; }
}

直到要求 #3 一切都很好。你的第四个要求

  1. My Class InfoWeather can have one or zero InfoAppointment

需要确定您真的想要这个。然后你必须更新 InfoAppointmentInfoWeather 实体如下:

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"));

希望这对你有用!