我有一個在mongo db「computers」中的集合,我想要檢索collect並將其保存到sql server使用實體框架。問題是集合有一個數組對象。我想將它轉換爲字符串並保存。請幫助,謝謝。 SQL服務器跳過字符串[]憑證並插入其餘部分。如何將字符串數組屬性轉換爲字符串保存在sql server中作爲逗號分隔的行mongo db到sql server C#
蒙戈DB收集
{
"_id": ObjectId('57852fcsdsdsdsd2662a0ce400'),
"machineName": "AAADESKTOP",
"updated": ISODate('2017-05-17T15:09:39.399Z'),
"__v": 111,
"credentials": [
"####################",
"####################",
"#################",
"#####################"
],
"clientVersion": "2.12.2",
"lastActivity": ISODate('2017-05-17T16:30:50.165Z'),
"secret": "####################"
}
**C# code**
var client = new MongoClient();
var db = client.GetDatabase("Computerdata");
var collection = db.GetCollection<Computers>("computers");
var data = collection.Find(new BsonDocument()).ToListAsync().Result;
foreach (var item in data)
{
EntityFramewrokContext.Computers.Add(item);
EntityFramewrokContext.SaveChanges();
}
public class Computers
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[Key]
public string Id { get; set; }
public string machineName { get; set; }
[BsonDateTimeOptions(Representation = BsonType.DateTime)]
public DateTime updated { get; set; }
public int version { get; set; }
public string[] credentials { get; set; }
public string clientVersion { get; set; }
[BsonDateTimeOptions(Representation = BsonType.DateTime)]
public DateTime lastActivity { get; set; }
public string secretKey { get; set; }
}
不確定你在這裏需要什麼幫助。但是不要像這樣存儲分隔數據。它違反了1NF並導致無數的痛苦。 –
請閱讀關於數據庫規範化https://en.wikipedia.org/wiki/Database_normalization –
實體框架跳過「字符串[]憑證」以下不保存「憑據」:[ 「######### ###########「, 」####################「, 」########## ###########「 ] –