我有一个类似的XML结构 XML Structure
我正在使用Entity和DBContext
将这个XML中的数据插入表中。
我的实体:
[Serializable]
public class Root
{
[Key]
public int Key1 { get; set; }
public int Key2 { get; set; }
[XmlElement("Parent")]
[ForeignKey("Key2")]
public List<Parent> liParent{ get; set; }
}
public class Parent
{
[Key]
public int Key1 { get; set; }
public int Key2 { get; set; }
[XmlElement("child")]
[ForeignKey("Key2")]
public List<child> lichild{ get; set; }
}
public class child
{
[Key]
public int Key1 { get; set; }
public int object1{get; set;}
public int object2 {get; set;}
}
在上面的Root只是用来维护一个基于父的列表并分配seqno,我不想将Root中的数据插入到任何表中。但是在执行datacontext时,它试图将数据插入到类名为“Roots”的表中并返回错误:
无效的对象名称Roots
如何消除考虑插入Root的代码?
DBContextCode:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<DBContext>(null);
modelBuilder.Entity<Parent>().ToTable("Table1");
modelBuilder.Entity<child>().ToTable("Table2");
}
XML处理代码:
Root clsRoot;
XmlSerializer serializer = new XmlSerializer(typeof(Root));
using (StreamReader reader = new StreamReader(sFilePath))
{
clsRoot = (Root)serializer.Deserialize(reader);
}
int RNo = 1;
clsRoot.liParent.ForEach(X =>
{
X.RecNo = RNo++;
int sRNo = 1;
X.lichild.ForEach(x =>
{
x.RecNo = sRNo++;
x.ParentRecNo = X.RecNo;
});
});
using (var dbContext = new DBContext())
{
dbContext.Root.Add(clsRoot);
dbContext.SaveChanges();
}