forked from timabell/ef-enum-to-lookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnumExample.cs
104 lines (90 loc) · 2.93 KB
/
EnumExample.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
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
using System;
using System.Data.Entity;
using EfEnumToLookup.LookupGenerator;
using NUnit.Framework;
namespace ExampleUsage
{
/// <summary>
/// Example usage of the ef lookup generator.
/// To see the library in action create a project for this example cs
/// add this file to it, add EF 6.1 and NUnit through nuget and run the
/// below test method with NUnit.
///
/// To see the generated schema:
/// Open up (localdb)\v11.0 in Sql Server Object Explorer
/// and look inside the new database it has generated.
/// Take a look at the Enum_Size table and its contents.
/// </summary>
[TestFixture]
public class EnumExample
{
[SetUp]
public void Setup()
{
Database.SetInitializer(new DropCreateDatabaseAlways<MyDbContext>());
}
[Test]
public void ExampleOfUsingApply()
{
using (var context = new MyDbContext())
{
var enumToLookup = new EnumToLookup();
enumToLookup.NameFieldLength = 42; // optional, example of how to override default values
// This would normally be run inside either a db initializer Seed()
// or the migration Seed() method which both provide access to a context.
enumToLookup.Apply(context);
}
}
[Test] public void ExampleOfGeneratingSql()
{
using (var context = new MyDbContext())
{
var enumToLookup = new EnumToLookup();
// if you need to get at the raw sql to run a migration separately then use:
var migrationSql = enumToLookup.GenerateMigrationSql(context);
// you'd probably want to write this to a file and then add it to source control, but for
// the purpose of demonstration we'll write it to the console instead:
Console.Out.WriteLine(migrationSql);
// at some point you'd then run the sql (probably not like this, but this serves as a test that it's working)
context.Database.ExecuteSqlCommand(migrationSql);
}
}
}
/// <summary>
/// Example context
/// </summary>
public class MyDbContext : DbContext
{
public DbSet<Foo> Foos { get; set; }
}
/// <summary>
/// Example entity that references an enum
/// </summary>
public class Foo
{
public int Id { get; set; }
public Size Size { get; set; }
public Shape Shape { get; set; }
}
/// <summary>
/// Example enum that will be converted into a lookup table.
/// </summary>
public enum Size
{
Small = 1, //db friendly id
// this is only fully qualified because of a name clash with NUnit, you wouldn't normally need to.
[System.ComponentModel.Description("It's average")] // example of apostrophe that would need escaping in sql
Medium,
ReallyVeryBig,
// this is only fully qualified because of a name clash with NUnit, you wouldn't normally need to.
[System.ComponentModel.Description("Huge you know?")] // give it a different name in the lookup table
Huge,
[RuntimeOnly] // this won't exist in the database, handy to prevent unwanted data creeping in (enforced by foreign key constraint).
Undecided
}
public enum Shape
{
Square,
Round
}
}