Skip to content

Instantly share code, notes, and snippets.

@jahands
Last active August 29, 2015 14:16
Show Gist options
  • Save jahands/45fc4bf880c6b267009c to your computer and use it in GitHub Desktop.
Save jahands/45fc4bf880c6b267009c to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Imflow.Utilities;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
namespace Imflow.Data.Entity.Configurations
{
class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
// Only the last configuration change is kept.
// So this will overwrite global conventions,
// and I can override anything configured by interfaces by adding
// them after this method call.
EntityConfiguration.ConfigureAny(typeof(User), this);
}
}
}
namespace Imflow.Data.Entity.Configurations
{
static class DefaultInterfaceConfiguration
{
/// <summary>
/// Method to configure a model inheriting from an interface
/// </summary>
public static void ConfigureAny<T>
(Type modelType, EntityTypeConfiguration<T> entity) where T : class
{
dynamic typeToConfigure = entity;
// Configure all of the interfaces
foreach (Type type in modelType.GetInterfaces())
{
if (type == typeof(IPerson))
ConfigureIPerson(typeToConfigure);
else if (type == typeof(IUserCredentials))
ConfigureIUserCredentials(typeToConfigure);
}
}
/// <summary>
/// Configuration for IPerson
/// </summary>
public static void ConfigureIPerson<T>(EntityTypeConfiguration<T> entity) where T : class, IPerson, new()
{
entity.Property(t => t.FirstName).HasMaxLength(15);
}
/// <summary>
/// Configuration for IUserCredentials
/// </summary>
public static void ConfigureIUserCredentials<T>
(EntityTypeConfiguration<T> entity) where T : class, IUserCredentials, new()
{
entity.Property(t => t.PasswordHash)
.HasColumnType("BINARY")
.HasMaxLength(64);
entity.Property(t => t.PasswordSalt)
.HasColumnType("BINARY")
.HasMaxLength(32);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment