-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDemoDbContext.cs
More file actions
31 lines (26 loc) · 989 Bytes
/
DemoDbContext.cs
File metadata and controls
31 lines (26 loc) · 989 Bytes
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
using DevExtremeVSTemplateMVC.Models;
using Microsoft.EntityFrameworkCore;
namespace DevExtremeVSTemplateMVC.DAL
{
public class DemoDbContext : DbContext {
public DbSet<EmployeeTask> Tasks { get; set; }
public DbSet<Contact> Contacts { get; set; }
public DbSet<TaskList> TaskLists { get; set; }
public DemoDbContext(DbContextOptions<DemoDbContext> options)
: base(options) { }
void GenerateTaskId() {
var newTasks = ChangeTracker.Entries<EmployeeTask>()
.Where(e => e.State == EntityState.Added);
foreach (var entry in newTasks) {
if (entry.Entity.TaskId == 0) {
var maxOrder = Math.Max(1000, Tasks.Max(t => t.TaskId));
entry.Entity.TaskId = maxOrder + 1;
}
}
}
public override int SaveChanges() {
GenerateTaskId();
return base.SaveChanges();
}
}
}