0

我想知道是否可以使用基类(抽象)中定义的虚拟属性来创建与其他具体类型的链接。

例如 :

public abstract class AbstractService
{
   public int Id {get;set;}

   public int? SiteId {get;set;}
   public virtual Site Site {get;set;}
}

public class StudyTeamService : AbstractService
{
   public int? RoleId {get;set;}
   public virtual Role Role {get;set;}
}


public abstract class AbstractSite
{
   public int Id {get;set;}

   public string Name {get;set;}
}

public class Site : AbstractSite
{
   public virtual ICollection<StudyTeamService> StudyTeamServices {get;set;}
}

我想我必须在 ICollection 上添加一个注释,以便它知道如何正确映射,但我找不到正确的。

你们有什么想法吗?

似乎如果我在 ICollection 上设置 [InverseProperty("Site")],它会崩溃并出现错误,表明该关系未在程序集中定义...

4

1 回答 1

1

我认为您的代码的问题在于

public class Site : AbstractSite
{
   public virtual ICollection<StudyTeamService> StudyTeamServices {get;set;}
}

因为关系是Site andAbstractService , not withStudyTeamService` 之间的关系。

下面的代码对我有用:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;

namespace StackOverflow
{

    public abstract class AbstractService
    {
        public int Id { get; set; }
        public virtual Site Site { get; set; }

        public int SiteId { get; set; }
    }

    [Table("StudyTeamServices")]
    public class StudyTeamService : AbstractService
    {
        public virtual Role Role { get; set; }
    }

    public class Role
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }


    public abstract class AbstractSite
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Site : AbstractSite
    {
        public virtual ICollection<AbstractService> StudyTeamServices { get; set; }
    }

    public class Context : DbContext
    {
        static Context()
        {
            Database.SetInitializer<Context>(null);
        }

        public DbSet<AbstractService> AbstractServices { get; set; }
        public DbSet<StudyTeamService> StudyTeamServices { get; set; }
        public DbSet<Site> Sites { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Not EdmMetadata Table on DB
            //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

            modelBuilder.Configurations.Add(new AbstractServiceMap());
        }

    }

    public class AbstractServiceMap : EntityTypeConfiguration<AbstractService>
    {
        public AbstractServiceMap()
        {
            HasRequired(a => a.Site).WithMany(s => s.StudyTeamServices).HasForeignKey(a => a.SiteId);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var context = new Context();
            context.Database.Delete();
            context.Database.Create();

            var studyTeamService = new StudyTeamService();
            studyTeamService.Role = new Role { Name = "role1" };
            studyTeamService.Site = new Site { Name = "Site1" };

            context.StudyTeamServices.Add(studyTeamService);
            context.SaveChanges();
            Console.WriteLine("Done");
            Console.ReadLine();
        }
    }
}
于 2012-04-13T10:29:43.427 回答