C# 如何连接Azure联合根数据库并在实体框架DBContext中应用联合?
我正在使用Asp.NETWebAPI、实体框架和SQLAzure。在我的解决方案中,我有从数据库生成的.edmx文件。下面的代码行是上下文实体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
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;