Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
100 lines (97 loc) · 3.58 KB
/
Copy pathProgram.cs
File metadata and controls
100 lines (97 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Linq;
namespace EFDuplicateProblem
{
class Program
{
static void Main(string[] args)
{
using (var context = new OrdersContext())
{
try
{
Database.SetInitializer(new CreateDatabaseIfNotExists<OrdersContext>());
context.Database.Initialize(false);
MoveAnItem(context);
}
catch (DataException ex)
{
if (ex.InnerException == null)
Console.WriteLine("DataException: " + ex.Message);
else
Console.WriteLine(string.Format("DataException: {0}, inner {1}: {2}",
ex.Message, ex.InnerException.GetType().FullName,
ex.InnerException.Message));
return;
}
catch (SqlException ex)
{
Console.WriteLine("SqlException: " + ex.Message);
return;
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().FullName + ": " + ex.Message);
return;
}
}
}
private static void MoveAnItem(OrdersContext db)
{
if (db.OrderItems.Count<OrderItem>() <= 2)
{
Console.WriteLine("No data");
return;
}
try
{
OrderItem ItemMoving = db.OrderItems.OrderByDescending(p => p.ListIndex).FirstOrDefault();
Console.WriteLine($"Moving Id: {ItemMoving.Id}, ListIndex: {ItemMoving.ListIndex}, Name: {ItemMoving.Name}");
IEnumerable<OrderItem> items =
from s in db.OrderItems
orderby s.ListIndex
select s;
Console.WriteLine("\tData before resequence");
foreach (OrderItem item in items)
Console.WriteLine($"{item.Id} {item.ListIndex} {item.Name}");
int newx = 1;
ItemMoving.ListIndex = newx;
foreach (OrderItem s in items)
{
if (s.Id != ItemMoving.Id)
s.ListIndex = ++newx;
}
Console.WriteLine("\tData before SaveChanges");
IEnumerable<DbEntityEntry> TrackEntries = db.ChangeTracker.Entries();
foreach (DbEntityEntry e in TrackEntries)
{
OrderItem s;
if ((s = e.Entity as OrderItem) is OrderItem)
Console.WriteLine($"{s.Id} {s.ListIndex} {e.State} {s.Name}");
else
Console.WriteLine($"Type {e.GetType().Name} is not OrderItem");
}
db.SaveChanges();
}
catch (Exception ex)
{
WalkExceptions(ex);
}
}
private static void WalkExceptions(Exception ex)
{
Console.WriteLine("\tException tree:");
Console.WriteLine(ex.GetType().FullName + ": " + ex.Message);
while (ex.InnerException != null)
{
ex = ex.InnerException;
Console.WriteLine(ex.GetType().FullName + ": " + ex.Message);
}
}
}
}