【问题标题】:EF 5 Add-Migration create a foreign key for GET only property?EF 5 Add-Migration 只为 GET 属性创建外键?
【发布时间】:2013-01-02 10:11:10
【问题描述】:

这是我的代码

图库类

public class Gallery
{
   public Guid Id { get; set; }
   public virtual ICollection<PhotoGallery> Photos { get; set; }
   public List<PhotoGallery> ActivePhotos 
   { 
      get { return this.Photos.Where(/*condition*/); 
   }
}

照片类

public class Photo
{
   public Guid Id { get; set; }
   public string PhotoPath { get; set; }
}

相册类

public class PhotoGallery
{
   public virtual Gallery Gallery { get; set; }
   public virtual Photo Photo { get; set; }
   public int SortOrder { get; set; }
}

如果我运行 Add-Migration 命令,它将为 Gallery.ActivePhotos 生成我不想要的关系。我的问题是,这是 EF5 的默认行为吗?在此之前,我记得像这样的 GET only 属性根本不会被映射。还是我错了?

【问题讨论】:

    标签: c# entity-framework migration


    【解决方案1】:

    尝试像这样将注释添加到您的属性中:

    public class Gallery
    {
       public Guid Id { get; set; }
       public virtual ICollection<PhotoGallery> Photos { get; set; }
    
       [NotMapped]
       public List<PhotoGallery> ActivePhotos 
       { 
          get { return this.Photos.Where(/*condition*/); 
       }
    }
    

    我没有发现任何说这对 EF5 来说是新的,但我使用的是 EF 4.3.1,并且只读取了像您的问题中没有映射的属性。

    希望对您有所帮助。

    【讨论】:

    • 谢谢@ben-tidman,但这就是我正在做的。只是对这种行为感到好奇。
    猜你喜欢
    • 2021-12-28
    • 2019-05-18
    • 2015-07-20
    • 2015-06-03
    • 2023-03-09
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    相关资源
    最近更新 更多