你的数据库
在实体框架中,您将拥有以下内容:
class Root
{
public int Id {get; set;}
... // other properties
// every Root has zero or more As (one-to-many):
public virtual ICollection<A> As {get; set;}
}
class A
{
public int Id {get; set;}
... // other properties
// every A belongs to exactly one Root, using foreign key
public int RootId {get; set;}
public virtual Root Root {get; set;}
// every A has zero or more Bs:
public virtual ICollection<B> Bs {get; set;}
}
class B
{
public int Id {get; set;}
... // other properties
// every B belongs to exactly one A, using foreign key
public int AId {get; set;}
public virtual A A {get; set;}
// every B has zero or more Cs:
public virtual ICollection<C> Cs {get; set;}
}
etc.
在实体框架中,表的列由非虚拟属性表示。虚拟属性表示表之间的关系(一对多,多对多)
你的档案
您提到可能是您已经从文件中读取了Root1,并再次与其他子级一起读取Root1。你的文件似乎是平的:
Root1, A1, B1, ...
Root1, A1, B2, ...
Root1, A2, B3, ...
Root2, A3, B4, ...
Root1, A4, B5, ...
class Line
{
public Root Root {get; set;}
public A A {get; set;}
public B B {get; set;}
...
}
IEnumerable<Line> ReadLines(...) {...}
填充数据库
void FillDatabase(IEnumerable<Line> lines)
{
// convert the lines into a sequence or Roots with their As,
// with their Bs, with their Cs, ...
IEnumerable<Root> roots = lines.GroupBy(line => line.Root,
(root, linesOfThisRoot) => new Root
{
Name = root.Name,
... other root properties
As = linesOfThisRoot.GroupBy(line => line.A,
(a, linesWithRootAndA) => new A
{
Name = a.Name,
... other a properties
Bs = linesWithRootAndA.GroupBy(line => line.B,
(b, linesWithRootAB) => new B
{
... B properties
Cs = linesWithRootAB.GroupBy(line => line.C,
{
etc.
})
.ToList(),
})
.ToList(),
})
.ToList(),
})
.ToList();
});
using (var dbContext = new MyDbContext()
{
dbContext.Roots.AddRange(roots.ToList());
dbContext.SaveChanges();
}
}