我有以下的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; }
}
是怎样的数据库结构生成?我认为你在某处调用了“CreateTable(type)”?如果你这样做,也许你错过了一个CreateTable(typeof(Project)); – TomTom
@TomTom:的确如此。我在底部添加了该方法。 – Robert