适用于 .net 的 DynamoDB:Table 数据模型中的名称
DynamoDB for .net: Table name in data model
我在我的应用程序中使用 Dynamodb.net。
我有以下代码。
var creds = new BasicAWSCredentials(awsId, awsPassword);
var dynamoClient = new AmazonDynamoDBClient(creds,
awsDynamoDbRegion);
var context = new DynamoDBContext(dynamoClient);
List<ScanCondition> conditions = new List<ScanCondition>();
conditions.Add(new ScanCondition("Id", ScanOperator.Equal, myId));
var response = await context.ScanAsync<Data>(conditions).GetRemainingAsync();
return response;
我的数据模型如下:
[DynamoDBTable("MyTable")]
public class Data
{
[DynamoDBHashKey]
public string Id{ get; set; }
public string Name { get; set; }
}
我们正在将模型 "Data" 中的 table table 名称编码为
[DynamoDBTable("MyTable")]
我们怎么能不硬编码呢。是否可以在我的实际代码中应用 table 名称而不是在模型中给出?
谢谢
DynamoDBOperationConfig
中的 OverrideTableName
是您要查找的内容吗?
描述:
Property that indicates the table to save an object to overriding the
DynamoDBTable attribute declared for the type.
示例:
var x = await DbContext.LoadAsync<T>("hash", new DynamoDBOperationConfig {
OverrideTableName = "NewTableName",
IndexName = indexName
});
此外,您正在寻找的可能是每个 DbContext 请求的 table 前缀。它会将此前缀附加到每个 table。如果你想隔离应用程序特定的 tables 像 AppName-MyTable...
很有用
示例:
return new DynamoDBContextConfig
{
TableNamePrefix = "MyAppIdentifier",
ConsistentRead = false,
};
另一种选择是通过以下方式为表格使用不同的前缀
Amazon.DynamoDBv2.DataModel.DynamoDBContextConfig.TableNamePrefix
Property that directs DynamoDBContext to prefix all table names with a specific string. > If property is null or empty, no prefix is used and default table names are used.
https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/DynamoDBv2/TDynamoDBContextConfig.html
例如:
var credentials = new StoredProfileAWSCredentials("default");
var client = new AmazonDynamoDBClient(credentials, RegionEndpoint.USEast1);
var config = new DynamoDBContextConfig{
TableNamePrefix = "Test."
};
var ctx = new DynamoDBContext(client, config);
await ctx.SaveAsync(item);
我在我的应用程序中使用 Dynamodb.net。
我有以下代码。
var creds = new BasicAWSCredentials(awsId, awsPassword);
var dynamoClient = new AmazonDynamoDBClient(creds,
awsDynamoDbRegion);
var context = new DynamoDBContext(dynamoClient);
List<ScanCondition> conditions = new List<ScanCondition>();
conditions.Add(new ScanCondition("Id", ScanOperator.Equal, myId));
var response = await context.ScanAsync<Data>(conditions).GetRemainingAsync();
return response;
我的数据模型如下:
[DynamoDBTable("MyTable")]
public class Data
{
[DynamoDBHashKey]
public string Id{ get; set; }
public string Name { get; set; }
}
我们正在将模型 "Data" 中的 table table 名称编码为
[DynamoDBTable("MyTable")]
我们怎么能不硬编码呢。是否可以在我的实际代码中应用 table 名称而不是在模型中给出?
谢谢
DynamoDBOperationConfig
中的 OverrideTableName
是您要查找的内容吗?
描述:
Property that indicates the table to save an object to overriding the DynamoDBTable attribute declared for the type.
示例:
var x = await DbContext.LoadAsync<T>("hash", new DynamoDBOperationConfig {
OverrideTableName = "NewTableName",
IndexName = indexName
});
此外,您正在寻找的可能是每个 DbContext 请求的 table 前缀。它会将此前缀附加到每个 table。如果你想隔离应用程序特定的 tables 像 AppName-MyTable...
很有用示例:
return new DynamoDBContextConfig
{
TableNamePrefix = "MyAppIdentifier",
ConsistentRead = false,
};
另一种选择是通过以下方式为表格使用不同的前缀
Amazon.DynamoDBv2.DataModel.DynamoDBContextConfig.TableNamePrefix
Property that directs DynamoDBContext to prefix all table names with a specific string. > If property is null or empty, no prefix is used and default table names are used.
https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/DynamoDBv2/TDynamoDBContextConfig.html
例如:
var credentials = new StoredProfileAWSCredentials("default");
var client = new AmazonDynamoDBClient(credentials, RegionEndpoint.USEast1);
var config = new DynamoDBContextConfig{
TableNamePrefix = "Test."
};
var ctx = new DynamoDBContext(client, config);
await ctx.SaveAsync(item);