-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModels.cs
More file actions
159 lines (133 loc) · 4.06 KB
/
Models.cs
File metadata and controls
159 lines (133 loc) · 4.06 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.Core.Common;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.SQLite;
using System.Data.SQLite.EF6;
using System.Linq;
using System.Web;
namespace BlogService
{
public class BaseModel
{
[Key]
public string Id { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public BaseModel()
{
Id = Guid.NewGuid().ToString();
CreatedAt = DateTime.Now;
UpdatedAt = DateTime.Now;
}
}
public class User : BaseModel
{
public string Username { get; set; }
public virtual ICollection<Blog> Blogs { get; set; }
}
public class UserInfo
{
public string Id { get; set; }
public string Username { get; set; }
public int Blogs { get; set; }
public UserInfo()
{
Username = null;
Blogs = -1;
}
}
public class Blog : BaseModel
{
public virtual User Author { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public virtual IList<Comment> Comments { get; set; }
}
public class BlogInfo
{
public string Id { get; set; }
public string Title { get; set; }
public int CommentNumber { get; set; }
public BlogInfo()
{
Id = null;
Title = null;
CommentNumber = -1;
}
public BlogInfo(Blog blog)
{
Title = blog.Title;
CommentNumber = blog.Comments.Count;
}
}
public class BlogDetail
{
public string Id { get; set; }
public string Author { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string UpdateTime { get; set; }
public CommentInfo[] Comments { get; set; }
public BlogDetail() { }
public BlogDetail(Blog blog)
{
Author = blog.Author.Username;
Title = blog.Title;
Content = blog.Content;
CommentInfo[] CommentInfos = blog.Comments.Select(x => new CommentInfo(x)).ToArray();
Comments = CommentInfos;
}
}
public class Comment : BaseModel
{
public virtual User Author { get; set; }
public virtual Blog Blog { get; set; }
public string Content { get; set; }
}
public class CommentInfo
{
public string Author { get; set; }
public string Content { get; set; }
public CommentInfo()
{
}
public CommentInfo(Comment comment)
{
Author = comment.Author.Username;
Content = comment.Content;
}
}
public class WebServiceDbContext : DbContext
{
public WebServiceDbContext() :
base(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;Connect Timeout=30")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Comment>()
.HasRequired(x => x.Blog)
.WithMany(x => x.Comments)
.WillCascadeOnDelete();
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Comment> Comments { get; set; }
public override int SaveChanges()
{
var entries = ChangeTracker.Entries().ToList();
var updateEntries = entries.Where(e => (e.Entity is BaseModel)
&& e.State == EntityState.Modified).ToList();
updateEntries.ForEach(e =>
{
((BaseModel)e.Entity).UpdatedAt = DateTime.Now;
});
return base.SaveChanges();
}
}
}