【问题标题】:multiple parent Entities with one child entity in EF code firstEF 代码中具有一个子实体的多个父实体
【发布时间】:2012-11-08 14:25:36
【问题描述】:

有两个实体 PostPhoto 。两者都有comment实体的集合。

有没有办法避免下面写的mapping(必须为Comment实体中的每个父级定义一个属性)?

public class Post
{
    public string Title {get; set;}
    public ICollection<Comment> Comments{get; set; }
}

public class Photo
{
    public string Path{get;set;}
    public ICollection<Comment> Comments{get; set; }
}

public class Comment
{
    public int? PostId{get;set;}
    public Virtual Post Post{get;set;}

    public int? PhotoId{get;set;}
    public Virtual Photo Photo{get;set;}
}

【问题讨论】:

    标签: c# entity-framework parent-child


    【解决方案1】:

    你可以这样做,

    public class PostBase{
        public ICollection<Comment> Comments{get; set; }
    }
    
    
    public class Post:PostBase
    {
        public string Title {get; set;}
    
    }
    
    public class Photo:PostBase
    {
        public string Path{get;set;}
    
    }
    
    public class Comment
    {
        public int? PostBaseId{get;set;}
        public Virtual PostBase PostBase{get;set;}
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 2014-06-03
      • 1970-01-01
      • 2017-10-16
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多