【发布时间】:2014-04-25 21:43:58
【问题描述】:
我能够通过使用用户导入 xml 文件来反序列化我的数据。现在,我想知道如何使用 Entity Framework/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; }
}
我的数据上下文:
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。我认为这与我将日期数组传递到数据库但数据库不理解数组有关。
如何修复我的类,以便将数据数组传递到数据库中?
如果您有任何问题,请告诉我。谢谢。
【问题讨论】:
-
你能在你尝试的地方显示代码吗
-
用数据库连接上下文更新了代码?
-
您是否尝试过使用
ICollection代替数组,然后在构造函数中对其进行初始化? -
在上面我使用数组的情况下,它说:对象引用未设置为对象的实例。我不确定如何使用 ICollection 以及在数组上使用 ICollection 有什么好处?
-
另外,当我尝试使用 ICollection 时,由于某种原因我无法让我的反序列化方法工作,我不确定为什么。它虽然适用于数组。我认为这可能是因为 XmlSerializer 不知道使用 ICollection 查找哪些标签。就我而言,我使用了 [XmlArray],它能够解析 xml 数据。当我将 [XmlElement] 用于 ICollection 时,我的反序列化方法会中断。我想这就是原因。
标签: c# sql xml entity-framework