[英]How to Use The Same Table With Different Entities In EF6
I have entity DbDropPhoto with navigation property我有具有导航属性的实体 DbDropPhoto
public class DbDropPhoto
{
public virtual DbContour Contour { get; set; }
}
public class DbContour
{
public virtual DbDropPhoto CurrentDropPhoto { get; set; }
}
Relationship configured like this using FluentAPI:使用 FluentAPI 像这样配置关系:
modelBuilder.Entity<DbContour>()
.HasRequired(s => s.CurrentDropPhoto)
.WithOptional(s => s.Contour)
.WillCascadeOnDelete();
now I need to add same DbContour entity to another entity called DbThermalPhoto.现在我需要将相同的 DbContour 实体添加到另一个名为 DbThermalPhoto 的实体中。 What is the best way to do it.最好的方法是什么。 Should I create separate table for example DbThermalPhotoContour or I can somehow re-use existing table.我应该创建单独的表,例如 DbThermalPhotoContour,还是可以以某种方式重新使用现有表。 I've searched for similar questions but solution to them seemed to be very complicated.我已经搜索过类似的问题,但它们的解决方案似乎非常复杂。
I managed to achieve it like this:我设法像这样实现它:
modelBuilder.Entity<DbDropPhoto>()
.HasOptional(c => c.Contour)
.WithMany()
.HasForeignKey(s => s.ContourId);
modelBuilder.Entity<DbThermalPhoto>()
.HasOptional(c => c.Contour)
.WithMany()
.HasForeignKey(s => s.ContourId);
And Entities:和实体:
[Table("DropPhotos")]
public class DbDropPhoto
{
[Key] public Guid PhotoId { get; set; }
public Guid? ContourId { get; set; }
[ForeignKey("ContourId")]
public virtual DbContour Contour { get; set; }
}
[Table("ThermalPhotos")]
public class DbThermalPhoto
{
[Key] public Guid PhotoId { get; set; }
public Guid? ContourId { get; set; }
[ForeignKey("ContourId")]
public virtual DbContour Contour { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.