sqlite - Xamarin 表单 sql lite 表添加外键约束
问题描述
我的应用程序使用要添加外键约束的模型创建表。
这是基表:
public class BaseTable
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Name { get; set; }
}
这是子表
public class ChildTable
{
[ForeignKeyConstraint(BaseTable.ID, ID)]
public int ID { get; set; }
public string Name { get; set; }
}
在我的 App.xaml.cs 中,我正在创建数据库,如下所示:
database.CreateTable<BaseTable>();
database.CreateTable<ChildTable>();
对于这一行:
[ForeignKeyConstraint(BaseTable.ID, ID)]
我得到对象 ref 是必需的,这是有道理的,但是我想在模型子表中添加外键关系。
在此先感谢您提供有关如何完成此任务的任何建议
解决方案
安装 SQLiteNetExtensions 后,我能够执行以下操作来解决我的问题:
public class ChildTable
{
[ForeignKey(typeof(BaseTable))]
public int ID { get; set; }
public string Name { get; set; }
}
然后更新了我的基表,如下所示:
public class BaseTable
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Name { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<ChildTable> ChildTables { get; set; }
}
推荐阅读
- angular - 如果使用 selectedIndex,mat-stepper 下一步按钮不起作用
- python - 如何使用 selenium python 从具有不同选项卡的弹出窗口中提取数据
- java - 如何使用 GRPC 将对象作为服务响应对象传递?
- html - 在一个输入中显示两个正文日历
- java - Spring Boot 模型类中的下划线
- c# - NullReferenceException:对象引用未设置为 lin 25 的对象 Item..ctor(ItemObject 项)的实例
- vaadin - Vaadin Listener Server-side vs Element-API,首选用例是什么?
- c# - 发生翻转/24 小时后使用 SSO openID 配置失败的 Azure Stack 服务器
- javascript - 如何在 Vue 的下拉列表中显示 fethced 数据
- selenium - 需要有关编写在 ID 属性中包含动态值的 xpath 的帮助