sqlite - Sqlite/SQLServer 提供程序差异
问题描述
我在使用 sqlite 提供程序编写集成测试时遇到了一些麻烦。
如果我将实体树附加到我的 DbContext,我会破坏关系(与 SqlServer 提供程序相同)。
有关详细信息,请参阅示例。
为了精确起见,3 个表的 PK 为 Id + TenantId。TenantId 仅在 SaveChangesAsync 方法中设置,具体取决于连接的用户。
如果我想让它在 sqlite 上工作,我必须在 3 个对象上设置 TenantId,然后再将它们附加到上下文......为什么这与 SQL Server 提供程序不同?
public class Store {
public int Id { get; set; }
public int TenantId { get; set; }
public List<Product> Products { get; set; }
}
public class Product {
public int Id { get; set; }
public int TenantId { get; set; }
public Store Store { get; set; }
public int StoreId { get; set; }
public Category Category { get; set; }
public int CategoryId { get; set; }
}
public class Category {
public int Id { get; set; }
public int TenantId { get; set; }
public List<Product> Products { get; set; }
}
[Test]
public void Test_Create(){
var store = new Store();
store.Products = new List<Product>();
var product = new Product();
product.Category = new Category();
store.Products.Add(product);
dbContext.Stores.Add(store);
// fails, product is detached from the Store after attaching to DbContext
Assert.Single(store.Products);
}
解决方案
推荐阅读
- javascript - 物化 CSS 选项卡不显示滚动行为
- html - 如何禁用剑道网格复选框列?
- html - 无法从 Flicker 渲染图像
- scala - 尝试在解析器中创建一个内置的 null-to-enum 转换器
- python - Keras 中类别子部分的准确度指标
- android - 如何更新通知栏下载文件?
- triggers - 日志监控在 Zabbix 2.0.6 版本中不起作用
- java - 如何在 android 中使用改造访问 404 错误?
- ios - 如何在没有用户干预的情况下直接从深度链接打开应用程序
- java - 如何从Android中的firebase实时数据库中获取最后的数据