我目前正在使用EF访问Azure存储表:EntityFramework.AzureTableStorage 7.0.0-beta1
配置模型时,我已将TableEntity的timestamp属性映射到我的模型的属性:
public class Subscription
{
public string Environment { get; set; }
public string Name { get; set; }
public DateTimeOffset LastModification { get; set; }
...
...
}
public class EF7Context : DbContext
{
public DbSet<Subscription> Subscriptions { get; set; }
protected override void OnConfiguring(DbContextOptions options)
{
options.UseAzureTableStorage("MyconnectionString");
}
protected override void OnModelCreating(Microsoft.Data.Entity.Metadata.ModelBuilder modelBuilder)
{
// Configure the Azure Table Storage
modelBuilder.Entity<Subscription>()
.ForAzureTableStorage() // Data are stored in an Azure Table Storage.
.Table("SubscriptionDev") // Name of the Table in the Azure Storage Account
.Timestamp(s => s.LastModification) // Map the timestamp
.PartitionAndRowKey(s => s.Environment, s => s.Name); // Map the partition and the row key
}
}
当插入新实体时,每件事听起来都不错,我可以使用LastModification检索新创建的实体,但如果我实例化第二个DbContext,我就无法检索我的实体:
using (var context = new EF7Context())
{
// Create the entity
var subscription = new Subscription() {Environment = "Dev", Name = "subscriptionName" };
context.Subscriptions.Add(subscription);
context.SaveChanges();
// Retrieve the entity in the same context => I assume entity has been cached ?
subscription = context.Subscriptions.SingleOrDefault(s => s.Name == "subscriptionName");
}
// Create new instance of the DbContext
using (var context = new EF7Context())
{
// Exception thrown here
var subscription = context.Subscriptions.SingleOrDefault(s => s.Name == "subscriptionName");
}
异常详情:
Microsoft.WindowsAzure.Storage.StorageException未处理 的HResult = -2146233088 Message =无法从'13'读取'DateTimeOffset'类型的值 源= Microsoft.WindowsAzure.Storage
从Azure存储帐户检索数据时,映射似乎不起作用。
有没有人知道解决方法(除了删除时间戳映射)?
答案 0 :(得分:2)
如前所述,EF Azure Table Storage beta1是原型,目前已停产。另见https://stackoverflow.com/a/35071077/2526265。它没有得到积极的发展(在撰写本文时)。