1

我正在为XAMARIN Forms project (PCL)使用SQLiteNetExtension。我面临的问题是在关系的多端定义的多对一关系的 ForeignKey 在第二个_connectionToDB.UpdateWithChildren()之后被 Null 覆盖:

  • 第一次插入正确
  • 第二次插入(相同的值):插入成功但 ForeignKey 为 NULL
  • 第三数据处理:数据库保存 4 行,其中 2 个外键为 NULL,另外 2 个正确
  • 第四数据处理:数据库中的 4 行有一个 NULL FK

(通过数据处理,我的意思是每次单击按钮时执行相同的代码 ..Example )

让我们来看看代码:

public class Bus
{
    [PrimaryKey, NotNull, Unique] //it's not AutoIncrement because it's unique 
    public String Id { get; set; }

    public string PlateNumber { get; set; }

    [OneToMany]
    public List<Person> Passengers { get; set; }
}

public class Person
{
    [PrimaryKey, NotNull, Unique]
    public int Id { get; set; }

    public string Name { get; set; }

    [ForeignKey(typeof(Bus))]
    public String BusId { get; set; }

    [ManyToOne]
    public Bus Bus { get; set; }
}

现在数据库和数据处理

var Persons = new List<Person>();
Bus B10 = new Bus {Id = "15458 ghf 14" ; PlateNumber = "122541 tn 154";}
_connectionToDB.InsertOrReplace(B10);

Person P1 = new Person{Id=15547; Name= Robert };
_connectionToDB.InsertOrReplace(P1);
Persons.Add(P1);

Person P2 = new Order {Id=25547;Name= Katherina};
_connectionToDB.InsertOrReplace(P2);
Persons.Add(P2);

B10.Passengers = Persons ;
_connectionToDB.UpdateWithChildren(B10);
4

0 回答 0