2017-07-18 88 views
0

我有以下的JSON的样板工程,这是我无法改变:C#反序列化,然后插入到SQLite数据库

[ 
    { 
     "id": 3, 
     "description": "Project A" 
    }, 
    { 
     "id": 6, 
     "description": "Project B", 
     "updated_at": "2017-07-13T09:30:51+02:00", 
     "created_at": "2017-07-13T09:30:51+02:00" 
    } 
] 

我有以下的JSON模式的任务,这是我无法改变:

[ 
    { 
     "id": 3, 
     "project": { 
      "id": 3, 
      "description": "Project A" 
     }, 
     "description": "Task 1" 
    }, 
    { 
     "id": 6, 
     "project": { 
      "id": 3, 
      "description": "Project A" 
     }, 
     "description": "Task 2" 
    }, 
    { 
     "id": 9, 
     "project": { 
      "id": 3, 
      "description": "Project A" 
     }, 
     "description": "Task 3" 
    }, 
    { 
     "id": 12, 
     "project": { 
      "id": 6, 
      "description": "Project B" 
     }, 
     "description": "Task 4" 
    } 
] 

我有以下2种型号:

using SQLite.Net.Attributes; 
using SQLiteNetExtensions.Attributes; 

namespace BettyMultiModel.Resources.Model 
{ 
    public class Models 
    { 

    } 

    class Project : Models 
    { 

     public int Id { get; set; } 
     public string Description { get; set; } 

    } 

    class Task : Models 
    { 

     public int Id { get; set; } 
     public string Description { get; set; } 

    } 
} 

我用这个方法来获取JSON到SQLite数据库:

// Insert Records of type TType into database. 
private async Task<string> InsertRecords<TType>(HttpResponseMessage response) 
     where TType : Models 
{ 
    try 
    { 
     DataBase db = new DataBase(); 

     string responseString = await response.Content.ReadAsStringAsync(); 
     var responseBody = JsonConvert.DeserializeObject<List<TType>>(responseString); 


     foreach (TType item in responseBody) 
     { 
      bool resultInsert = db.Insert(item); 
     } 

     return ResponseToMessage(response, "Insert Records"); 
    } 
    catch (Exception ex) 
    { 
     Log.Info("DeserializeObject", ex.Message); 
     throw; 
    }    

} 

时,我只希望有Id和两种模型的说明性质这一切正常。但我也想在Task模型中存储项目模型和任务模型/项目ID之间的关系。

当我在http://json2csharp.com/解析JSON与任务,我得到了模型的以下建议:

public class Project 
{ 
    public int id { get; set; } 
    public string description { get; set; } 
} 

public class RootObject // Which is Task 
{ 
    public int id { get; set; } 
    public Project project { get; set; } // <-------- Extra property 
    public string description { get; set; } 
} 

使用模型这样的 - 与RootObject更名为任务 - 在一个异常的结果: NotSupportedException异常:不知道BettyMultiModel.Resources.Model.Project

所以我改变了类:

using SQLite.Net.Attributes; 
using SQLiteNetExtensions.Attributes; 

namespace BettyMultiModel.Resources.Model 
{ 
    public class Models 
    { 

    } 

    class Project : Models 
    { 

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

    } 

    class Task : Models 
    { 

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

     [OneToMany] 
     public Project project { get; set; } 

    } 
} 

异常不再被抛出,但是当我查看db文件时,仍然只有Id和Description列,并且没有提及这两个模型之间的关系。

我在这里错过了什么?

编辑:创建表

public void ClearTable<TType>() 
     where TType : Models 
{ 
    try 
    { 
     Log.Info("Folder DB: ", dbPath); 

     using (var connection = new SQLiteConnection(platform, dbPath)) 
     { 
      connection.DropTable<TType>(); 
      connection.CreateTable<TType>(); 
     } 
    } 
    catch (SQLiteException ex) 
    { 
     Log.Info("SQLiteEx", ex.Message); 
    } 

} 

编辑添加方法:几乎没有...

我还做了一个小错误,一个项目有许多任务,所以该属性应该是[ManyToOne]。 db中的表现在看起来不错。项目ID只是没有插入任务模型中的ProjectId列中。每行都有值0.我想我需要弄清楚如何将嵌套的项目对象的id映射到类Task的ProjectId属性。

我试过使用这样的Newtonsoft.Json命名空间,它在Task模型中插入1行,然后由于记录ID的唯一性而开始抛出错误。JsonProperty( “ID”)将其标识为任务的ID,而不是项目:

[JsonProperty("id"), ForeignKey(typeof(Project))] 
public int ProjectId { get; set; } 

这些机型现在:

class Project : Models 
{ 

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

    public string Description { get; set; } 

} 

class Task : Models 
{ 

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

    public string Description { get; set; } 

    [ForeignKey(typeof(Project))] 
    public int ProjectId { get; set; } 

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

} 
+0

是怎样的数据库结构生成?我认为你在某处调用了“CreateTable(type)”?如果你这样做,也许你错过了一个CreateTable(typeof(Project)); – TomTom

+0

@TomTom:的确如此。我在底部添加了该方法。 – Robert

回答

1

假设,在你的问题你使用Twin Coders SQLite-Net-Extensions软件包我认为在定义模型方面存在错误。

如果你看一下许多在链接页面到位桶一个部分,你需要在除了ManyToOne属性来定义一个ForeignKey属性。这是他们的例子(注意在Person类总线和BusId属性):

public class Bus 
{ 
    [PrimaryKey, AutoIncrement] 
    public int Id { get; set; } 

    public string PlateNumber { get; set; } 
} 

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

    public string Name { get; set; } 

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

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

与您的代码,我相信所有你需要做的就是用下面的模型:

class Project : Models 
{ 
    [PrimaryKey] 
    public int Id { get; set; } 

    public string Description { get; set; } 

    // The id of the task used in the relationship (this gets stored in the DB) 
    [ForeignKey(typeof(Task))] 
    public int TaskId { get; set; } 

    // Meaning you have many projects to one task 
    [ManyToOne] 
    public Task Task { get; set; } 
} 

class Task : Models 
{ 
    [PrimaryKey] 
    public int Id { get; set; } 

    public string Description { get; set; } 
} 
+0

这非常接近解决方案。我与ManyToOne犯了一个小错误,这必须是OneToMany。我希望在上一期中看到我的文章底部的更新。谢谢! – Robert

+0

没问题。当然,属性应该是JsonProperty(“projectId”)? – TomTom

+0

另外,您更新的模型[ManyToOne]表示您对每个项目都有许多任务。这是期望的吗?如果你想为每个任务设置很多项目,我会像在答案中一样将外键移动到Project。 – TomTom