简体   繁体   English

EF多模式代码优先

[英]EF Multiple Schema CodeFirst

How to create tables with multiple schemas in EF with codefirst approach. 如何使用Codefirst方法在EF中创建具有多个架构的表。

i have this Context but it seems only the Schema.Legion is created. 我有此上下文,但似乎只创建了Schema.Legion。

any help would be appreciated. 任何帮助,将不胜感激。

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);
        }
    }
}

You have duplicated/reused the poco EntityNames. 您已经重复/重用了poco EntityNames。 Each entity is mapped to a table. 每个实体都映射到一个表。 It cant be mapped to mutliple tables. 它不能映射到多个表。 Which table should Context.Set<TPoco>() refer to when it is declared twice? 两次声明Context.Set<TPoco>()应该引用哪个表? In your case, The last definition is winning. 就您而言,最后的定义就是胜利。

You could 你可以

  1. create mutiple contexts. 创建多个上下文。 Each context has the enityt mapping required. 每个上下文都有所需的实体映射。 You can have more than 1 context open at a time. 一次可以打开多个上下文。 So you can read from one attach to the other. 因此,您可以从一个附件读取另一个附件。 or access 2 contexts. 或访问2个上下文。
  2. If you really need the tables inside one context. 如果您真的需要在一个上下文中使用表。 Then you will need to declare each entity. 然后,您将需要声明每个实体。 Perhaps using an abstract base. 也许使用抽象的基础。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM