Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF多模式代码优先_C#_Entity Framework_Ef Code First - Fatal编程技术网

C# EF多模式代码优先

C# EF多模式代码优先,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,如何使用codefirst方法在EF中创建具有多个模式的表 我有这个上下文,但似乎只有模式。军团被创建了 任何帮助都将不胜感激 namespace DotA.Server.Context { public class DotAContext : DbContext { public DbSet<EFHero> Hero { get; set; } public DbSet<EFItem> Item { get; set; }

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

我有这个上下文,但似乎只有模式。军团被创建了

任何帮助都将不胜感激

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);
        }
    }
}
名称空间DotA.Server.Context
{
公共类DotAContext:DbContext
{
公共数据库集英雄{get;set;}
公共数据库集项{get;set;}
公共数据库集技能{get;set;}
公共DbSet Stat{get;set;}
私有模式;
私有静态只读ConcurrentDictionary ModelCache=新ConcurrentDictionary();
公共DotAContext(模式)
:base(“name=DotAConnectionString”)
{
this.schema=schema;
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().ToTable(“Hero”,Schema.tribul.ToString());
modelBuilder.Entity();
modelBuilder.Entity().ToTable(“Item”,Schema.briath.ToString());
modelBuilder.Entity().ToTable(“Stat”,Schema.briath.ToString());
modelBuilder.Entity().ToTable(“Hero”,Schema.Legion.ToString());
modelBuilder.Entity().ToTable(“Skill”,Schema.Legion.ToString());
modelBuilder.Entity().ToTable(“Item”,Schema.Legion.ToString());
modelBuilder.Entity().ToTable(“Stat”,Schema.Legion.ToString());
基于模型创建(modelBuilder);
}
}
}

您已经复制/重用了poco实体名。 每个实体都映射到一个表。它不能映射到多个表。 当两次声明时,
Context.Set()
应该引用哪个表? 在你的例子中,最后一个定义是赢

你可以

  • 创建多个上下文。每个上下文都有所需的enityt映射。 一次可以打开多个上下文。 因此,您可以从一个附件读取另一个附件。或访问2个上下文
  • 如果您真的需要一个上下文中的表。然后需要声明每个实体。 也许是用一个抽象的基础

  • 不。。它起作用了。。我所做的是多方面的。。谢谢@phil