我要把我的数据模型移到Azure弹性秤上。
经过一些测试和一些经验,我爱上了如果它,它是简单的,用这种方法,代码保持干净和易于维护。
我有一个很大的问题,,切分键定义的在哪里?我找不到从Visual下载的示例的信息,我可以击败这是一个直接的答案。
在微软提供的示例中,默认的切分键是CustomerId,但我找不到对该键的引用发生在哪里。
它可能在ShardMapName中的配置文件中吗?
提前谢谢。
发布于 2015-05-04 17:30:00
SQL架构中的切分键与其使用(在代码中)之间没有显式链接。
因此,在入门示例中,Customers和Orders表都包含一个CustomerId列,您可以看到,在DataDependentRoutingSample.cs中,当我们访问这些表时,我们确保向下面的查询中为customerId列(包括SELECT和INSERT语句)使用的shardMap.OpenConnectionForKey方法提供相同的customerId值。
// Looks up the key in the shard map and opens a connection to the shard
using (SqlConnection conn = shardMap.OpenConnectionForKey(customerId, credentialsConnectionString))
{
// Create a simple command that will insert or update the customer information
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"
IF EXISTS (SELECT 1 FROM Customers WHERE CustomerId = @customerId)
UPDATE Customers
SET Name = @name, RegionId = @regionId
WHERE CustomerId = @customerId
ELSE
INSERT INTO Customers (CustomerId, Name, RegionId)
VALUES (@customerId, @name, @regionId)";
cmd.Parameters.AddWithValue("@customerId", customerId);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@regionId", regionId);
cmd.CommandTimeout = 60;
// Execute the command
cmd.ExecuteNonQuery();
}换句话说,当您在OpenConnectionForKey调用中提供某个键值时,您有责任确保所有具有该连接的SQL查询都仅限于该键值,否则您可能会得到不正确的结果(例如,如果它是一个SELECT查询)或驻留在错误碎片上的行(例如,如果它是一个插入查询)。
可以通过使用新的行级安全功能来解决这个安全问题。我们有一个名为实体框架多租户碎片的示例,它演示了如何将碎片地图与行级安全性相结合。相关代码在ElasticScaleContext.cs中
SqlConnection conn = null;
try
{
// Ask shard map to broker a validated connection for the given key
conn = shardMap.OpenConnectionForKey(shardingKey, connectionStr, ConnectionOptions.Validate);
// Set CONTEXT_INFO to shardingKey to enable Row-Level Security filtering
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"SET CONTEXT_INFO @shardingKey";
cmd.Parameters.AddWithValue("@shardingKey", shardingKey);
cmd.ExecuteNonQuery();
return conn;
}
catch (Exception)
{
if (conn != null)
{
conn.Dispose();
}
throw;
}谢谢你的好问题!
https://stackoverflow.com/questions/30027451
复制相似问题