1

这些是表格,以*结尾的字段是PK。在Image表中,HomeIDCarID 之一必须具有值,而另一个为空。与Feature中的类似, 它们是 0 而不是 null,因此可以将其用作 Key ( this.HasKey)。但是当我尝试映射时,在相关记录中找不到它们。例如Home.Images返回 0 条记录。

+---------------+ +-----------------+ +------------- --+ +----------------+ +----------------+
|首页 | |汽车 | |图片 | |特色 | |特征类型 |
|---------------| |-----------------| |---------------| |---------------| |---------------|
|家庭身份证* | |车牌* | |图像ID* | |FeatureTypeId* | |FeatureTypeId* |
|标题 | |品牌 | |姓名 | |家庭身份证* | |姓名 |
|说明 | |型号| |网址 | |车牌* | | |
|地址 | |年 | |家庭I​​D(空) | | | | |
| | | | |车牌号(空) | | | | |
+---------------+ +-----------------+ +------------- --+ +----------------+ +----------------+

这些是我的模型和映射的代码。

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

        }
     }
4

0 回答 0