blob: e7c606f1b2b1c43105c2896a47f9852d6211c45b (
plain) (
blame)
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
|
using System;
using DevHive.Data.Models;
using DevHive.Data.RelationModels;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace DevHive.Data
{
public class DevHiveContext : IdentityDbContext<User, Role, Guid>
{
public DevHiveContext(DbContextOptions<DevHiveContext> options)
: base(options) { }
public DbSet<Technology> Technologies { get; set; }
public DbSet<Language> Languages { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<UserFriends> UserFriends { get; set; }
public DbSet<Rating> Rating { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
/* User */
builder.Entity<User>()
.HasIndex(x => x.UserName)
.IsUnique();
/* Roles */
builder.Entity<User>()
.HasMany(x => x.Roles)
.WithMany(x => x.Users);
/* Friends */
builder.Entity<UserFriends>()
.HasKey(x => new { x.UserId, x.FriendId });
// builder.Entity<UserFriends>()
// .HasOne(x => x.Friend)
// .WithMany(x => x.Friends);
builder.Entity<User>()
.HasMany(x => x.Friends)
.WithOne(x => x.User);
/* Languages */
builder.Entity<User>()
.HasMany(x => x.Languages)
.WithMany(x => x.Users)
.UsingEntity(x => x.ToTable("LanguageUser"));
builder.Entity<Language>()
.HasMany(x => x.Users)
.WithMany(x => x.Languages)
.UsingEntity(x => x.ToTable("LanguageUser"));
/* Technologies */
builder.Entity<User>()
.HasMany(x => x.Technologies)
.WithMany(x => x.Users)
.UsingEntity(x => x.ToTable("TechnologyUser"));
builder.Entity<Technology>()
.HasMany(x => x.Users)
.WithMany(x => x.Technologies)
.UsingEntity(x => x.ToTable("TechnologyUser"));
/* Post */
builder.Entity<Post>()
.HasOne(x => x.Creator)
.WithMany(x => x.Posts);
builder.Entity<Post>()
.HasMany(x => x.Comments)
.WithOne(x => x.Post);
builder.Entity<Post>()
.HasOne(x => x.Rating)
.WithOne(x => x.Post);
/* Comment */
builder.Entity<Comment>()
.HasOne(x => x.Post)
.WithMany(x => x.Comments);
builder.Entity<Comment>()
.HasOne(x => x.Creator)
.WithMany(x => x.Comments);
base.OnModelCreating(builder);
}
}
}
|