Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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# 如何连接Azure联合根数据库并在实体框架DBContext中应用联合?_C#_Entity Framework_Azure_Ef Code First_Azure Sql Database - Fatal编程技术网

C# 如何连接Azure联合根数据库并在实体框架DBContext中应用联合?

C# 如何连接Azure联合根数据库并在实体框架DBContext中应用联合?,c#,entity-framework,azure,ef-code-first,azure-sql-database,C#,Entity Framework,Azure,Ef Code First,Azure Sql Database,我正在使用Asp.NETWebAPI、实体框架和SQLAzure。在我的解决方案中,我有从数据库生成的.edmx文件。下面的代码行是上下文实体 public partial class Entities : DbContext { public Entities(): base("name=Entities") { } protected override void OnModelCreating(Db

我正在使用Asp.NETWebAPI、实体框架和SQLAzure。在我的解决方案中,我有从数据库生成的.edmx文件。下面的代码行是上下文实体

public partial class Entities : DbContext     
{         
    public Entities(): base("name=Entities")
    {         }             
    protected override void OnModelCreating(DbModelBuilder modelBuilder)         
    {             throw new UnintentionalCodeFirstException();         }            
    public DbSet<Activity> Activities { get; set; }
}

在创建dbcontext的实例时,我想在Shardid的基础上建立新的连接。

在花费了太多时间之后,我想到了这一点。这对我来说很好

 var   customerEntity = new Entities(ConnectionStringCustomerDB());
                    var connection = new SqlConnection();
                    connection =(SqlConnection) customerEntity.Database.Connection;
                    connection.Open();
                    var command = new SqlCommand();                
                    string federationCmdText = @"USE FEDERATION Customer_Federation(ShardId =" + shardId + ") WITH RESET, FILTERING=ON";
                    command.Connection = connection;
                    command.CommandText = federationCmdText;
                    command.ExecuteNonQuery();
return Entities;
必须将EntityDatabase连接转换为SqlConnection,以正常方式执行federation命令

实体将对指定的shardId执行查询,并连接到正确的数据库

 var   customerEntity = new Entities(ConnectionStringCustomerDB());
                    var connection = new SqlConnection();
                    connection =(SqlConnection) customerEntity.Database.Connection;
                    connection.Open();
                    var command = new SqlCommand();                
                    string federationCmdText = @"USE FEDERATION Customer_Federation(ShardId =" + shardId + ") WITH RESET, FILTERING=ON";
                    command.Connection = connection;
                    command.CommandText = federationCmdText;
                    command.ExecuteNonQuery();
return Entities;