实体框架按GUID映射多个实体

时间:2013-11-05 12:53:27

标签: c# entity-framework mapping one-to-many

我有一些课程

public class Class1
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Guid Class1Id { get; set; }
    public List<MediaSet> MediaSet { get; set; }
}

public class Class2
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Guid Class2Id { get; set; }
    public List<MediaSet> MediaSet { get; set; }
}

public class MediaSet
{
    [Key]
    public int MediaSetId { get; set; }
    public Guid ObjectId { get; set; }
    public List<Media> Media { get; set; }
}

public enum MediaType { Photo, Video }

public class Media
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Guid MediaId { get; set; }
    public MediaType MediaType { get; set; }
    public string Url { get; set; }
    public int MediaSetId { get; set; }
    [ForeignKey("MediaSetId")]
    public MediaSet MediaSet { get; set; }
}

我想通过单个属性属性ObjectId将类MediaSet映射到Class1和Class2,我该怎么做? 所以稍后我可以执行以下查询

var classes1WithMediaSet = MyDbContext.Classes1.Include(x => x.MediaSet).ToList();
var classes2WithMediaSet = MyDbContext.Classes2.Include(x => x.MediaSet).ToList();

0 个答案:

没有答案