我能夠使用用戶導入XML文件反序列化我的數據。現在,我想知道如何使用實體框架/ ADO.NET將反序列化的XML數據存儲到本地數據庫中?如何獲取反序列化數據的數組並將其存儲到實體框架數據庫中?
我的模型:
[Serializable]
[XmlRoot("lot_information")]
public class LotInformation
{
[Key]
public int Id { get; set; }
[XmlArray("components")]
[XmlArrayItem("component", typeof(Components))]
public Components[] components { get; set; }
[XmlArray("families")]
[XmlArrayItem("control", typeof(Families))]
public Families[] families { get; set; }
[XmlAttribute("exp_date")]
public DateTime exp_date { get; set; }
[XmlAttribute("lot_number")]
public string lot_number { get; set; }
}
[Serializable]
public class Components
{
[Key]
public int Id { get; set; }
[XmlAttribute("control")]
public string aControl { get; set; }
[XmlAttribute("cal_ref")]
public string cal_ref { get; set; }
[XmlAttribute("family")]
public string family { get; set; }
[XmlAttribute("component")]
public int component { get; set; }
[XmlAttribute("id")]
public string componentId { get; set; }
[XmlElement("target")]
public int target { get; set; }
[XmlElement("min")]
public int min { get; set; }
[XmlElement("max")]
public int max { get; set; }
[XmlElement("number")]
public int number { get; set; }
}
[Serializable]
public class Families
{
[Key]
public int Id { get; set; }
[XmlAttribute("family")]
public string controlFamily { get; set; }
[XmlAttribute("pctrl")]
public string pctrl { get; set; }
}
我的DataContext:
class DMIDataContext : DbContext
{
public DbSet<Components> Components { get; set; }
public DbSet<Families> Families { get; set; }
public DbSet<ReagentLotInformation> ReagentLotInformation {get;set;}
public DMIDataContext() : base("DMIConnectionString")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
我的視圖模型: 摘錄與數據庫連接:
public static void DeSerializationXML(string filePath)
{
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "lot_information";
xRoot.IsNullable = false;
// Create an instance of lotinformation class.
var lot = new LotInformation();
// Create an instance of stream writer.
TextReader txtReader = new StreamReader(filePath);
// Create and instance of XmlSerializer class.
XmlSerializer xmlSerializer = new XmlSerializer(typeof(LotInformation), xRoot);
// DeSerialize from the StreamReader
lot = (LotInformation)xmlSerializer.Deserialize(txtReader);
// Close the stream reader
txtReader.Close();
using (var db = new DMIDataContext())
{LotInformation newLot = new LotInformation();
if (newLot != null)
{
newLot.Id = lot.Id;
newLot.lot_number = lot.lot_number;
newLot.exp_date = lot.exp_date;
for (int i = 0; i < lot.Components.Length; i++)
{
newLot.Components[i].aControl = lot.Components[i].aControl;
newLot.Components[i].cal_ref = lot.Components[i].cal_ref;
newLot.Components[i].family = lot.Components[i].family;
newLot.Components[i].component = lot.Components[i].component;
newLot.Components[i].componentId = lot.Components[i].componentId;
newLot.Components[i].target = lot.Components[i].target;
newLot.Components[i].min = lot.Components[i].min;
newLot.Components[i].number = lot.Components[i].number;
}
db.LotInformation.Add(newLot);
db.SaveChanges();
我遇到的問題是我能夠將exp_date和lot_number傳遞給我的表。這只是組件和家族的數組返回NULL。我認爲這與我將一個數組傳遞給數據庫有關,但數據庫不理解數組。
如何修復我的類,以便將數據數組傳遞到數據庫中?
如果您有任何問題,請讓我知道。謝謝。
你可以顯示代碼,你試試它 –
當然,嘗試什麼? –
用db連接上下文更新了代碼? –