-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBDesignEF.cs
More file actions
38 lines (33 loc) · 1.32 KB
/
Copy pathDBDesignEF.cs
File metadata and controls
38 lines (33 loc) · 1.32 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
using Microsoft.EntityFrameworkCore;
using DBDesign.Model;
namespace DBDesign
{
public class DBDesignEF : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Friend> Friends { get; set; }
public DbSet<UserMessage> UserMessages { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<UserGroup> UserGroups { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
const string _connectionString = "Your connection string here";
optionsBuilder.UseMySql(_connectionString, ServerVersion.AutoDetect(_connectionString));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Friend>()
.HasKey(f => new { f.User1Id, f.User2Id });
modelBuilder.Entity<UserGroup>()
.HasKey(ug => new { ug.UserId, ug.GroupId });
modelBuilder.Entity<UserGroup>()
.HasOne(ug => ug.User)
.WithMany(u => u.UserGroups)
.HasForeignKey(ug => ug.UserId);
modelBuilder.Entity<UserGroup>()
.HasOne(ug => ug.Group)
.WithMany(g => g.UserGroups)
.HasForeignKey(ug => ug.GroupId);
}
}
}