【问题标题】:Enforce foreign key constraint with SQLite & C#使用 SQLite 和 C# 强制外键约束
【发布时间】:2019-04-16 13:41:12
【问题描述】:

我在数据库中创建了一个外键约束ON DELETE SET DEFAULT,并分配了一个默认值。

我尝试了什么:

  • 在 App.Config 中:我在连接字符串中添加了“foreign keys=true;”。
  • I edited the dataset XSD
  • 我更改了 WinForm 上的 DataSet 并在那里强制执行:EnforceConstraints = true
  • 我检查了,我的 SQLite 版本足够高,可以支持这个(我使用 v1.0.110.0)
  • 我无法使用 PRAGMA 命令(如下所示:Enabling Foreign key constraints in SQLite),因为数据绑定到控件。
  • 我重建了项目。
  • 我在 SqliteStudio 中尝试过,约束有效,但在 C# 中无效。

我可能错过了什么......

更新: App.Config 以某种方式与项目设置不同步,因此额外的参数不包含在构建中。但是通过项目属性添加参数会引发错误。

"Data Source=.\InventoryManagerDatabase.db;foreign keys=true;" 

错误:

System.InvalidOperationException: '未知的连接字符串参数'外键'。'

显然这个参数不存在?我检查了各种资源,包括 Enabling Foreign key constraints in SQLite 和 SQLite EF6 programmatically set connection string at runtime 但这很奇怪...... SQLite 删除了这个参数还是重命名了它?

我也尝试过(以及区分大小写的组合):

EnforceFKConstraints=True;
foreign key=true;
foreign keys=true;
foreign_keys=true;
ForeignKeys=true;
Foreign Key Constraints=true;
PRAGMA foreign_keys = ON; // obviously does not work

但没有任何效果......

【问题讨论】:

    标签: c# sqlite


    【解决方案1】:

    终于找到了!

    解决方案:

    connectionString="Data Source=.\YourDatabaseName.db;Foreign Key Constraints=On;Version=3;"
    

    是的,他们在某处将其更改为 Foreign Key Constraints=On 并且 Google 上的每个页面(包括 StackOverflow)仍显示旧参数...

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2014-01-17
      • 2013-11-22
      • 2016-02-18
      • 2014-11-28
      • 2012-04-04
      • 2012-02-03
      • 2013-05-03
      相关资源
      最近更新 更多