我应该缓存传入的数据。我有2个模型OrderModel和OrderDetailedModel,其中OrderModel是基类,OrderDetailedModel是子类。
用户首先打开订单列表 - 仅检索一些数据(订单模型列表)。该列表应该被缓存。
当用户选择特定订单时,所有信息都是(OrderDetailedModel),这也应该被缓存。
重要的是,当再次下载列表时,我不希望OrderModel覆盖缓存的OrderDetailedModel行。 (不想清除详细信息)
我尝试创建2个不同的DbSet:
public DbSet<OrderModel> Orders { get; set; }
public DbSet<OrderDetailedModel> OrderDetails { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<OrderDetailedModel>().Map(m =>
{
//m.MapInheritedProperties(); // this doesn't help either
m.ToTable("OrderDetails");
});
modelBuilder.Entity<OrderModel>().Map(m =>
{
//m.MapInheritedProperties();
m.ToTable("Orders");
});
}
但似乎他们仍然以某种方式联系在一起。这是每个DbSet的“内容”:
订单:
FROM [Orders] AS [Extent1]
LEFT OUTER JOIN (SELECT
[Extent2].[Id] AS [Id],
.....
cast(1 as bit) AS [C1]
FROM [OrderDetails] AS [Extent2] ) AS [Project1] ON [Extent1].[Id] = [Project1].[Id]}
订单明细:
{SELECT
N'0X0X' AS [C1],
[Extent1].[Id] AS [Id],
...
[Extent1].[StoreContact] AS [StoreContact],
...
FROM [OrderDetails] AS [Extent1]
INNER JOIN [Orders] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]}
这会导致错误:
var a = Entry<T>(detailedOrderObject);
“System.InvalidOperationException:找到的实体是类型 请求类型为OrderDetailedModel的实体时的OrderModel。
这是因为详细订单与列表中下载的订单之一具有相同的Id广告。 (Ids来自服务器)
这是去哪里的正确方法?
修改
模特课程:
public class OrderModel
{
[Key, Column(Order=0), DatabaseGenerated(DatabaseGeneratedOption.None)]
[JsonProperty(PropertyName = "order_id")]
public int Id { get; set; }
[JsonProperty(PropertyName = "order_number")]
public String Number { get; set; }
//... only properties, like the one above
public class OrderDetailedModel :OrderModel
{
[JsonProperty(PropertyName="store_contact")]
public string StoreContact { get; set; }
//... only properties, like the one above
答案 0 :(得分:1)
您的类仍然是连接的,因为您的OrderDetailedModel类是从OrderModel派生的,当您查询表时,EF会感到困惑。
话虽这么说,我认为你应该把它们分成两个独立的表,如果没有其他原因除了规范化你的数据库。要将其拆分,您需要在代码中包含ForeignKey属性,并停止从OrderModel派生OrderDetailedModel类。
假设OrderModel:OrderDetailedModel是1:1,这就是你要做的:
将此添加到OrderModel:
public virtual OrderDetailedModel OrderDetails { get; set; }
对于你的OrderDetailedModel有以下内容(任何其他注释,如JsonProperty也可以保留 - 我只是向你展示你需要的EF):
public class OrderDetailedModel
{
[Key, ForeignKey("Order")]
public int OrderID { get; set; }
//some properties
public virtual OrderModel Order { get; set; }
}
让我知道它是如何运作的。
答案 1 :(得分:0)
为了防止有人需要继承,我发布了一个我创建的解决方法:
public class OrderModel
{
protected bool _detailed;
[Key, Column(Order=0), DatabaseGenerated(DatabaseGeneratedOption.None)]
[JsonProperty(PropertyName = "order_id")]
public int Id { get; set; }
[Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.None)]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public bool Detailed
{
get { return _detailed; }
set
{
if( (this.GetType() == typeof(OrderModel) && value== true) ||
(this.GetType() == typeof(OrderDetailedModel) && value==false))
throw new ArgumentException("This property is restricted");
_detailed = value;
}
}
//....
public OrderModel()
{
_detailed = false;
}
}
public class OrderDetailedModel :OrderModel
{
//.....
public OrderDetailedModel()
{
_detailed = true;
}
}
它不漂亮,但有效。