使用POCO对象的接口?

时间:2017-01-17 22:04:02

标签: c# entity-framework inheritance

我正在尝试将接口合并到我的最新应用程序中。对于简单的类,它似乎非常简单,但对于具有关系的类,事情似乎变得奇怪。使用具有关系的类的接口的正确方法是什么? 我的界面:

   public interface IAgent
    {
        string FirstName { get; set; }
        int Id { get; set; }
        string LastName { get; set; }
        IEnumerable<ITeam> Teams { get; set; }
    }

   public interface ITeam
    {
        string Name { get; set; }
        int Id { get; set; }
        IEnumerable<IAgent> Agents { get; set; }
    }

我的课程最终成为:

   public class Agent : IAgent
      { 
       public Agent()
        {
            Teams = new HashSet<Team>();
        }
        public string FirstName { get; set; }
        public int Id { get; set; }
        public string LastName { get; set; }
        public virtual IEnumerable<ITeam> Teams { get; set; }
      }
     public class Team : ITeam
      {
       public Team()
       {
        Agents = new HashSet<Agent>();
       }
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual IEnumerable<IAgent> Agents { get; set; }
      }

以这种方式结构化,POCO类不会使用代码优先迁移创建关系表。

我的上下文类:

  public class SDRContext :DbContext
{
    public SDRContext():base("SDRContext")
    {
    }
    public DbSet<Agent> Agents { get; set; }
    public DbSet<Team> Teams { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("SDR");
        base.OnModelCreating(modelBuilder);
    }
}

0 个答案:

没有答案