-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
53 lines (45 loc) · 1.25 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace EFOwned
{
class Program
{
static void Main(string[] args)
{
}
}
class MyDbContext : DbContext
{
public DbSet<Parent> Parents => Set<Parent>();
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options
.UseSqlServer("Server=server;Database=db;User Id=sa;Password = password;");
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Parent>(cfg =>
{
cfg.OwnsMany(e => e.Children, child =>
{
child.WithOwner()
.HasForeignKey(e => e.ParentId);
});
// cfg.HasMany(e => e.Children)
// .WithOne()
// .HasForeignKey(e => e.ParentId);
});
}
}
class Parent
{
public Guid Id { get; set; }
public List<Child> Children { get; set; }
}
class Child
{
public Guid Id { get; set; }
public Guid ParentId { get; set; }
}
}