Guest User

Untitled

a guest
Aug 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. Entity Framework code first woes
  2. public class A{
  3. [Key]
  4. string Name;
  5. B DefaultB;
  6. ICollection<B> Bs;
  7. }
  8.  
  9. public class B{
  10. [Key]
  11. int Key;
  12. }
  13.  
  14. public class C{
  15. [Key]
  16. string Key;
  17. A MyA;
  18. B MyB;
  19.  
  20. public C(A a,B b){
  21. MyA=a;
  22. MyB=b;
  23. }
  24. }
  25.  
  26. public class MyDbContext:DbContext{
  27. public DbSet<A> As
  28. public DbSet<B> Bs
  29. public DbSet<C> Cs
  30. }
  31.  
  32. main(){
  33. A a=new A();
  34. B b=new B();
  35. a.addB(B);
  36. MyDbContext m=new MyDbContext();
  37. m.As.Add(a);
  38. m.SaveChanges();
  39. C c=new C(a,b);
  40. m.Cs.Add(c);
  41. m.SaveChanges();
  42. }
  43.  
  44. A a=new A();
  45. B b=new B();
  46. a.addB(B);
  47.  
  48. using (MyDbContext m = new MyDbContext())
  49. {
  50. m.As.Add(a);
  51.  
  52. C c=new C(a,b);
  53. m.Cs.Add(c);
  54.  
  55. m.SaveChanges();
  56. }
  57.  
  58. A a=new A();
  59. B b=new B();
  60. a.addB(B);
  61.  
  62. using (MyDbContext m = new MyDbContext())
  63. {
  64. m.As.Add(a);
  65. m.SaveChanges();
  66. }
  67.  
  68. // ...
  69.  
  70. using (MyDbContext m2 = new MyDbContext())
  71. {
  72. m2.As.Attach(a);
  73. m2.Bs.Attach(b);
  74.  
  75. C c=new C(a,b);
  76. m2.Cs.Add(c);
  77.  
  78. m2.SaveChanges();
  79. }
Add Comment
Please, Sign In to add comment