Lets say you were using EF Core, given
Main table/model
public class Wine
{
public int WineId { get; set; }
public string Name { get; set; }
public WineType WineType { get; set; }
public override string ToString() => $"{WineType} {Name}";
}
Navigation
[Table("WineType")]
public class WineTypes
{
[Key]
public int Id { get; set; }
public string TypeName { get; set; }
public string Description { get; set; }
}
DbContext
public class WineContext : DbContext
{
public DbSet<Wine> Wines { get; set; }
public DbSet<WineTypes> WineTypes { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=EF.Wines;Trusted_Connection=True");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Wine>()
.Property(e => e.WineType)
.HasConversion<int>();
modelBuilder.Entity<WineTypes>().HasData(
new WineTypes() {Id = 1, TypeName = "Red", Description = "Classic red"},
new WineTypes() {Id = 2, TypeName = "White", Description = "Dinner white"},
new WineTypes() {Id = 3, TypeName = "Rose", Description = "Imported rose"}
);
modelBuilder.Entity<Wine>().HasData(
new Wine() { WineId = 1, Name = "Veuve Clicquot Rose", WineType = WineType.Red },
new Wine() { WineId = 2, Name = "Whispering Angel Rose", WineType = WineType.Rose },
new Wine() { WineId = 3, Name = "Pinot Grigi", WineType = WineType.White },
new Wine() { WineId = 4, Name = "White Zinfandel", WineType = WineType.Rose }
);
}
}
It is not possible to set WineType property of Wine to an int but the value is stored as an int which is proper as if the string value changes that creates issues so to fix this we use a .tt file to recreate WineType.
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="EnvDTE.dll" #>
<#@ Assembly Name="System.Data" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#
var tableName = Path.GetFileNameWithoutExtension(Host.TemplateFile);
var path = Path.GetDirectoryName(Host.TemplateFile);
var columnId = "Id";
var columnName = "TypeName";
var columnDescription = "Description";
var connectionString = "Server=(localdb)\\mssqllocaldb;Database=EF.Wines;Trusted_Connection=True";
IServiceProvider serviceProvider = (IServiceProvider)Host;
DTE dte = (DTE)serviceProvider.GetService(typeof(DTE));
Project project = dte.Solution.FindProjectItem(Host.TemplateFile).ContainingProject;
#>
using System.CodeDom.Compiler;
using System.ComponentModel;
namespace <#= project.Properties.Item("DefaultNamespace").Value #><#= Path.GetDirectoryName(Host.TemplateFile).Remove(0, Path.GetDirectoryName(project.FileName).Length).Replace("\\", ".") #>
{
/// <summary>
/// <#= tableName #> auto generated enumeration
/// </summary>
[GeneratedCode("TextTemplatingFileGenerator", "10")]
public enum <#= tableName #>
{
<#
SqlConnection conn = new SqlConnection(connectionString);
string command = $"select {columnId}, {columnName}, {columnDescription} from {tableName} order by {columnId}";
SqlCommand comm = new SqlCommand(command, conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
bool loop = reader.Read();
while(loop)
{
#> [Description("<#= reader[columnDescription] #>")]
<#= Pascalize(reader[columnName]) #> = <#= reader[columnId] #><# loop = reader.Read(); #><#= loop ? ",\r\n" : string.Empty #><#
}
#>
}
}
<#+
private string Pascalize(object value)
{
Regex rx = new Regex(@"(?:[^a-zA-Z0-9]*)(?<first>[a-zA-Z0-9])(?<reminder>[a-zA-Z0-9]*)(?:[^a-zA-Z0-9]*)");
return rx.Replace(value.ToString(), m => m.Groups["first"].ToString().ToUpper() + m.Groups["reminder"].ToString().ToLower());
}
#>