View difference between Paste ID: Aj47DqyV and qzhkueKe
SHOW: | | - or go back to the newest paste.
1
//Item.cs
2
    public class Item
3
    {
4
        public Item()
5
        {
6-
            Composites = new HashSet<Item>();
6+
            Composites = new HashSet<ItemItem>();
7-
            Components = new HashSet<Item>();
7+
            Components = new HashSet<ItemItem>();
8
        }
9-
    
9+
10
        public int ItemId { get; set; }
11
12
        public string Name { get; set; }
13
14
        public int Cost { get; set; }
15
16-
        public virtual ICollection<Item> Composites { get; set; }
16+
        public virtual ICollection<ItemItem> Composites { get; set; }
17
18-
        public virtual ICollection<Item> Components { get; set; }
18+
        public virtual ICollection<ItemItem> Components { get; set; }
19
20
        public void AddComponent(MyContext context, Item item, [Range(1, Int32.MaxValue)] int quantity = 1)
21
        {
22
            var itemRelation = Components.FirstOrDefault(c => c.Composite == this);
23
            if (itemRelation == null)
24
            {
25
                itemRelation = new ItemItem() { Composite = this, Component = item, Quantity = quantity };
26
                this.Components.Add(itemRelation);
27
                item.Composites.Add(itemRelation);
28
            }
29
            else
30
            {
31-
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
31+
                itemRelation.Quantity += quantity;
32
            }
33-
            modelBuilder.Entity<Item>()
33+
34-
                .HasMany(x => x.Composites)
34+
35-
                .WithMany(x => x.Components);
35+
        public void AddComposite(MyContext context, Item item, [Range(1, Int32.MaxValue)] int quantity = 1)
36
        {
37
            var itemRelation = Composites.FirstOrDefault(c => c.Component == this);
38
            if (itemRelation == null)
39
            {
40-
    public partial class MainWindow : Window
40+
                itemRelation = new ItemItem() { Component = this, Composite = item, Quantity = quantity };
41
                this.Composites.Add(itemRelation);
42
                item.Components.Add(itemRelation);
43
            }
44
            else
45
            {
46
                itemRelation.Quantity += quantity;
47
            }
48
        }
49
    }
50
51
//ItemItem.cs
52
    public class ItemItem
53
    {
54
        public ItemItem()
55
        {
56
57-
                Item i3 = new Item() { Name = "System Unit", Cost = 250 };
57+
58
59
        [Key]
60
        public int RelationId { get; set; }
61
62
        public int Quantity { get; set; }
63
64-
                
64+
        public virtual Item Composite { get; set; }
65-
                i3.Components.Add(i1);
65+
66-
                i3.Components.Add(i2);
66+
        public virtual Item Component { get; set; }
67-
                i1.Composites.Add(i3);
67+
68-
                i2.Composites.Add(i3);
68+
69
//MyContext.cs
70
    public class MyContext : DbContext
71
    {
72
        public MyContext()
73
            :base("DbConnection")
74
        { }
75
76
        public DbSet<Item> Items { get; set; }
77
78
        public DbSet<ItemItem> ItemItems { get; set; }
79
    }
80
81
//MainWindow.xaml.cs
82
public partial class MainWindow : Window
83
    {
84
        public MainWindow()
85
        {
86
            InitializeComponent();
87
88
            AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory);
89
        }
90
91
        private void Window_Loaded(object sender, RoutedEventArgs e)
92
        {
93
            Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>());
94
95
            using (MyContext context = new MyContext())
96
            {
97
98
                Item i1 = new Item() { Name = "Motherboard", Cost = 500 };
99
                Item i2 = new Item() { Name = "Network Adapter", Cost = 150 };
100
                Item i3 = new Item() { Name = "System Unit", Cost = 0 };
101
102
                context.Items.Add(i1);
103
                context.Items.Add(i2);
104
                context.Items.Add(i3);
105
106
                context.SaveChanges();
107
108
                i3.AddComponent(context, i1);
109
                i3.AddComponent(context, i2);
110
111
                context.SaveChanges();
112
            }
113
        }
114
    }