2014-04-25 37 views
0

我能夠使用用戶導入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。我認爲這與我將一個數組傳遞給數據庫有關,但數據庫不理解數組。

如何修復我的類,以便將數據數組傳遞到數據庫中?

如果您有任何問題,請讓我知道。謝謝。

+0

你可以顯示代碼,你試試它 –

+0

當然,嘗試什麼? –

+0

用db連接上下文更新了代碼? –

回答

1

你可以試試這個: 可以使用ListICollection

[Serializable] 
[XmlRoot("lot_information")] 
public class LotInformation 
{ 

    Public LotInformation() 
    { 
    Components = new List<Components>(); 
    Families = new List<Families>(); 
    } 

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

[XmlArray("components")] 
[XmlArrayItem("component", typeof(Components))] 
public List<Components> Components { get; set; } 

[XmlArray("families")] 
[XmlArrayItem("control", typeof(Families))] 
public List<Families> Families { get; set; } 

.... 

} 

並在您的視圖模型來代替:

using (var db = new DMIDataContext()) 
    {LotInformation newLot = new LotInformation(); 

      newLot.Id = lot.Id; 
      newLot.lot_number = lot.lot_number; 
      newLot.exp_date = lot.exp_date; 

     foreach (Components comp in lot.Components) 
      { 

         newLot.Components.Add(comp); 

      } 
     foreach (Families fam in lot.Families) 
      { 

         newLot.Families.Add(fam); 

      } 
      db.LotInformation.Add(newLot); 
      db.SaveChanges(); 

我希望這將有助於。 正如你在這個link中看到的,你可以使用List。

+0

編輯:嗨,再次,我想出了你做錯了什麼!您將公開信息寫爲「公開」,但您忘記將其添加到LotInformation方法中:Public LotInformation() { Components = new List (); 家庭=新列表(); }。否則,它的作品!非常感謝!!這讓我感到沮喪!非常感謝你的幫助!!說真的,我花了這麼多小時:D –

+1

我會更新'()'。我很高興它幫助你。 –

相關問題