EF多模式代码优先

时间:2013-12-02 03:59:55

标签: c# entity-framework ef-code-first

如何使用codefirst方法在EF中创建具有多个模式的表。

我有这个上下文,但似乎只创建了Schema.Legion。

任何帮助将不胜感激。

namespace DotA.Server.Context
{
    public class DotAContext : DbContext
    {
        public DbSet<EFHero> Hero { get; set; }
        public DbSet<EFItem> Item { get; set; }
        public DbSet<EFSkill> Skill { get; set; }
        public DbSet<EFStat> Stat { get; set; }

        private Schema schema;
        private static readonly ConcurrentDictionary<Tuple<string, string>, DbCompiledModel> ModelCache = new ConcurrentDictionary<Tuple<string, string>, DbCompiledModel>();

        public DotAContext(Schema schema)
            : base("name=DotAConnectionString")
        {
            this.schema = schema;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<EFHero>().ToTable("Hero", Schema.Scourge.ToString());
            modelBuilder.Entity<EFSkill>().ToTable("Skill", Schema.Scourge.ToString());
            modelBuilder.Entity<EFItem>().ToTable("Item", Schema.Scourge.ToString());
            modelBuilder.Entity<EFStat>().ToTable("Stat", Schema.Scourge.ToString());

            modelBuilder.Entity<EFHero>().ToTable("Hero", Schema.Legion.ToString());
            modelBuilder.Entity<EFSkill>().ToTable("Skill", Schema.Legion.ToString());
            modelBuilder.Entity<EFItem>().ToTable("Item", Schema.Legion.ToString());
            modelBuilder.Entity<EFStat>().ToTable("Stat", Schema.Legion.ToString());

            base.OnModelCreating(modelBuilder);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您已复制/重复使用poco EntityNames。 每个实体都映射到一个表。它不能映射到mutliple表。 什么时候Context.Set<TPoco>()引用哪个表引用它? 在你的情况下,最后的定义是赢。

你可以

  1. 创建多个上下文。每个上下文都需要enityt映射。 您可以一次打开多个上下文。 所以你可以从一个附着到另一个附着。或访问2个上下文。
  2. 如果你真的需要一个上下文中的表。然后,您需要声明每个实体。 也许使用抽象基础。
相关问题