-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test1.cs
64 lines (56 loc) · 1.83 KB
/
Test1.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
54
55
56
57
58
59
60
61
62
63
64
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
namespace Spike.EFCore9RowVersion;
public class Contact
{
public int Id { get; set; }
[MaxLength(100)]
public string Name { get; set; } = "";
[MaxLength(100)]
public string Email { get; set; } = "";
[MaxLength(20)]
public string Phone { get; set; } = "";
}
public class Person
{
public int Id { get; set; }
[MaxLength(100)]
public string FirstName { get; set; } = "";
[MaxLength(100)]
public string LastName { get; set; } = "";
public IList<Contact> Contacts { get; set; } = [];
public byte[] RowVersion { get; set; }
}
public class TestDbContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
public DbSet<Person> Persons { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=.;Database=TestDb;Trusted_Connection=True;TrustServerCertificate=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>(entity =>
{
entity.ToTable( t => t.IsTemporal());
entity.Property(e => e.RowVersion).IsRowVersion();
});
}
}
[TestClass]
public sealed class Test1
{
[TestMethod]
public void Should_Migrate_Database()
{
using var context = new TestDbContext();
context.Database.EnsureDeleted();
context.Database.Migrate();
context.Persons.Add(new Person { FirstName = "John", LastName = "Doe", Contacts = [new Contact { Name = "Jane Doe", Email = "[email protected]" }] });
context.SaveChanges();
}
}