积极答复者
怎么更新链接表呢?

问题
答案
-
您好,请参考以下代码:
namespace CodeFirst { class Program { static void Main(string[] args) { using (TestContext context = new TestContext()) { context.Database.Create(); A a = new A(); a.Id = 1; B b = new B(); b.Id = 1; a.bList.Add(b); C c = new C(); a.cList.Add(c); b.cList.Add(c); context.aSet.Add(a); context.SaveChanges(); } } } class A { public int Id { get; set; } public List<C> cList { get; set; } public List<B> bList { get; set; } public A() { cList = new List<C>(); bList = new List<B>(); } } class B { public int Id { get; set; } public List<C> cList { get; set; } public List<A> aList { get; set; } public B() { cList = new List<C>(); aList = new List<A>(); } } class C { public int Id { get; set; } public A a { get; set; } public B b { get; set; } } class TestContext : DbContext { public DbSet<A> aSet { get; set; } public DbSet<B> bSet { get; set; } public DbSet<C> cSet { get; set; } } }
Allen Li [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Allen_MSDNModerator 2012年6月8日 1:57
全部回复
-
-
您好,请参考以下代码:
namespace CodeFirst { class Program { static void Main(string[] args) { using (TestContext context = new TestContext()) { context.Database.Create(); A a = new A(); a.Id = 1; B b = new B(); b.Id = 1; a.bList.Add(b); C c = new C(); a.cList.Add(c); b.cList.Add(c); context.aSet.Add(a); context.SaveChanges(); } } } class A { public int Id { get; set; } public List<C> cList { get; set; } public List<B> bList { get; set; } public A() { cList = new List<C>(); bList = new List<B>(); } } class B { public int Id { get; set; } public List<C> cList { get; set; } public List<A> aList { get; set; } public B() { cList = new List<C>(); aList = new List<A>(); } } class C { public int Id { get; set; } public A a { get; set; } public B b { get; set; } } class TestContext : DbContext { public DbSet<A> aSet { get; set; } public DbSet<B> bSet { get; set; } public DbSet<C> cSet { get; set; } } }
Allen Li [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Allen_MSDNModerator 2012年6月8日 1:57