这些是表格,以*结尾的字段是PK。在图片表格中, HomeID 或 CarID 中的一个必须具有该值,而另一个为null。与功能中的内容类似
它们是0而不是null,因此可以将它用作键(this.HasKey
)。但是当我试图映射时,在相关记录中找不到它们。例如,Home.Images
返回0记录。
+---------------+ +-----------------+ +---------------+ +---------------+ +---------------+ |Home | |Car | |Image | |Feature | |FeatureType | |---------------| |-----------------| |---------------| |---------------| |---------------| |HomeID* | |CarID* | |ImageID* | |FeatureTypeId* | |FeatureTypeId* | |Title | |Brand | |Name | |HomeID* | |Name | |Description | |Model | |URL | |CarID* | | | |Address | |Year | |HomeID(null) | | | | | | | | | |CarID (null) | | | | | +---------------+ +-----------------+ +---------------+ +---------------+ +---------------+
这些是我的模型和映射的代码。
public class BaseModel
{
/// <summary>
/// Gets or sets Id
/// </summary>
public int Id { get; set; }
}
public class Home : BaseModel
{
/*All direct fields*/
public List<Image> Images{get; set;}
}
internal class HomeMap : EntityTypeConfiguration<Home>
{
public HomeMap()
{
/* All direct mapping to Properties to ColumnsNames */
// I tried the following 2 lines
//this.HasOptional(p => p.Images).WithRequired().WillCascadeOnDelete(false);
//this.HasOptional(p => p.Images).WithMany().Map(m => m.MapKey("ImageId")).WillCascadeOnDelete(false);
}
}
/* All the same definition and mapping for the others*/
/* ImageMap for the relation*/
/* Image model has a field type `Home` name **Home** */
internal class ImageMap: EntityTypeConfiguration<Image>
{
public ImageMap()
{
/* All direct mapping to Properties to ColumnsNames */
//Currently this is what I am using for the mapping
this.HasOptional(i => i.Home).WithMany(h => h.Images).HasForeignKey(i => i.HomeID).WillCascadeOnDelete(false);
}
}