0

尝试创建具有多值外键的一对多关系时出现以下错误...

**

SQLite.Net.SQLiteException:外键不匹配 - “活动”引用“节点”

**

这是表 SQL(为清楚起见进行了简化)...

CREATE TABLE [Activity] (
  [ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
  [InstId_FK] numeric(18,0) NOT NULL, 
  [NodeSeq_FK] numeric(3,0) NOT NULL, 
  [ActivityType] int NOT NULL, 
  CONSTRAINT [Node_FK] FOREIGN KEY([InstId_FK], [NodeSeq_FK]) REFERENCES [Node]([ID], [NodeSeq])ON DELETE CASCADE);

CREATE TABLE [Node] (
  [ID] NUMERIC(18) NOT NULL CONSTRAINT [ID] REFERENCES [Trip]([ID]) ON DELETE CASCADE, 
  [NodeSeq] numeric(3,0) NOT NULL, 
  [Status] int, 
  [Name] nvarchar(30) NOT NULL, 
  CONSTRAINT [sqlite_autoindex_TripNode_1] PRIMARY KEY ([ID], [NodeSeq]));

还有我的 C# 模型(为清楚起见再次简化)...

public class TripNodeActivity : LocationAware
{
    [PrimaryKey, AutoIncrement, ForeignKey(typeof(Node),Order = 1)]
    public int Id { get; set; }

    [Column("InstId_FK")]
    public int InstIdFk { get; set; }

    [Column("NodeSeq_FK"), ForeignKey(typeof(Node),Order = 2)]
    public int NodeSeqFk { get; set; }

    public int ActivityType { get; set; }
}

public class Node
{
    public Node(){Activity = new List<Activity>();}

    [OneToMany(CascadeOperations = CascadeOperation.All)] 
    public List<Activity> Activity{get;set;}  

    [PrimaryKey, ForeignKey(typeof(Trip))]
    public int Id { get; set; }

    [PrimaryKey]
    public int NodeSeq { get; set; }

    public int Status { get; set; }
    public String Name { get; set; }
}

尝试使用以下方法保存模型时会发生此错误:

SQLiteConnection.InsertOrReplaceWithChildren(NodeFromAbove,true)
4

1 回答 1

1

那里的东西很少。

首先,您在类中有两个PrimaryKey属性。NodeSQLite-Net 不支持这种方式。

其次,您指定 PrimaryKey 也是两个类中另一个表的 ForeignKey。那不是你想做的。

可能这更有可能是您正在尝试的:

public class TripNodeActivity : LocationAware
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [Column("InstId_FK")]
    public int InstIdFk { get; set; }

    [Column("NodeSeq_FK"), ForeignKey(typeof(Node)]
    public int NodeSeqFk { get; set; }

    public int ActivityType { get; set; }
}

public class Node
{
    public Node(){Activity = new List<Activity>();}

    [OneToMany(CascadeOperations = CascadeOperation.All)] 
    public List<Activity> Activity{get;set;}  

    [PrimaryKey]
    public int Id { get; set; }

    public int NodeSeq { get; set; }

    public int Status { get; set; }
    public String Name { get; set; }
}

希望能帮助到你。

于 2014-08-13T15:50:26.857 回答