【发布时间】:2019-03-05 15:56:03
【问题描述】:
Xamarin.Forms 项目,使用 SQLiteNetExtensions 2.0.0 和 sqlite-net-pcl 1.5.231
我正在尝试将新记录插入到 sqlite 数据库中。当我在子表上删除 NOT NULL 和外键约束时,我的场景有效。当我重新添加约束时,我得到以下异常:
SQLite.SQLiteException: '约束'
- 父模型
[Table("RetailItem")]
public class RetailItemDTO
{
[PrimaryKey, AutoIncrement]
public int RetailItemId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Taxable { get; set; }
public int DepartmentId { get; set; }
public int RetailTypeId { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<RetailItemRetailerPriceDTO> Prices { get; set; }
}
- 儿童模型
[Table("RetailItemRetailerPrice")]
public class RetailItemRetailerPriceDTO
{
[PrimaryKey, AutoIncrement]
public int RetailItemRetailerPriceId { get; set; }
[ForeignKey(typeof(RetailItemDTO)), NotNull]
public int RetailItemId { get; set; }
public int RetailerId { get; set; }
public decimal Price { get; set; }
}
- 插入/更新逻辑
public async Task Save(RetailItem entity)
{
var dto = GetDTOFromBusinessModel(entity);
if (dto.RetailItemId == 0)
{
await _sqliteRepository.Database.InsertWithChildrenAsync(dto, true);
entity.Id = dto.RetailItemId;
}
else
await _sqliteRepository.Database.InsertOrReplaceWithChildrenAsync(dto, true);
}
如果我有一个现有的 RetailItem,并带有新添加的价格,则 InsertOrReplaceWithChildrenAsync 调用 正常工作 失败并显示以下内容:SQLite.NotNullConstraintViolationException: 'NOT NULL constraint failed: RetailItemRetailerPrice.RetailItemId'
每当我使用新的 RetailItemRetailerPrice 添加新的 RetailItem 时,都会发生约束异常。
如果我在 RetailItemRetailerPrice 表上删除 RetailItemId 的 NOT NULL 和外键约束,那么这两个调用都可以工作。
我想在我的桌子上保持适当的约束。
我是否遗漏了一些可以使我的场景正常工作的属性?
【问题讨论】:
标签: xamarin.forms