Map Table / Columns动态实体框架

时间:2014-01-16 19:08:54

标签: c# .net entity-framework dynamic

我尝试动态地使用ef 4将列映射到属性,但我不知道该怎么做。

我的类有数据注释引用像这样的表列名。

[Table(Name = "TB_CLIENTE_HUB")]
public class ClienteRepository : Repository<ClienteRepository>, IDisposable
{
    [Column(Name = "ID_CLIENT")]
    public int Id { get; set; }
    [Column(Name = "NOME")]
    public string Nome { get; set; }
    [Column(Name = "SOBRENOME")]
    public string Sobrenome { get; set; }
    [Column(Name = "CPF")]
    public string CPF { get; set; }
    [Column(Name = "EMAIL")]
    public string Email { get; set; }
    [Column(Name = "SEXO")]
    public string Sexo { get; set; }
    [Column(Name = "DT_NASCIMENTO")]
    public DateTime? DataNascimento { get; set; }
    [Column(Name = "ESTADO_CIVIL")]
    public string EstadoCivil { get; set; }
    [Column(Name = "SENHA")]
    public string Senha { get; set; }
    [Column(Name = "DDD")]
    public string DDD { get; set; }
    [Column(Name = "CELULAR")]
    public string Celular { get; set; }
    [Column(Name = "TIPO_LOGRADOURO")]
    public string TipoLogradouro { get; set; }
    [Column(Name = "LOGRADOURO")]
    public string Logradouro { get; set; }
    [Column(Name = "NUMERO")]
    public string Numero { get; set; }
    [Column(Name = "COMPLEMENTO")]
    public string Complemento { get; set; }
    [Column(Name = "BAIRRO")]
    public string Bairro { get; set; }
    [Column(Name = "CIDADE")]
    public string Cidade { get; set; }
    [Column(Name = "CEP")]
    public string CEP { get; set; }
    [Column(Name = "UF")]
    public string UF { get; set; }
    [Column(Name = "DT_CADASTRO")]
    public DateTime? DataCadastro { get; set; }
    [Column(Name = "DT_ATUALIZACAO")]
    public DateTime? DataAtualizacao { get; set; }
    [Column(Name = "FG_ATIVO")]
    public bool? IsAtivo { get; set; }
    [Column(Name = "DT_ATIVACAO")]
    public DateTime? DataAtivacao { get; set; }
    [Column(Name = "FG_PRIMEIRO_ACESSO")]
    public bool? IsPrimeiroAcesso { get; set; }
    [Column(Name = "ID_FACEBOOK")]
    public string FacebookId { get; set; }
}

所以在我的DbContext中我有以下几行......

public class DAOContext : DbContext
{
    public DAOContext() : base("name=defaultConnection")
    {
        Database.SetInitializer<DAOContext>(null);            
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var maps = Util.GetMapping();            
        foreach (var map in maps)
        {

            var config = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(map.ClassType).Invoke(modelBuilder, null);

            config.GetType().InvokeMember("ToTable", BindingFlags.InvokeMethod, null, config,
                                                        new object[] {map.TableName});

            var hasKey = config.GetType().GetMethod("HasKey");
            var paramEx = Expression.Parameter(map.ClassType, "m");
            var lambdaEx = Expression.Lambda(Expression.Property(paramEx, "Id"), paramEx);

            hasKey.MakeGenericMethod(typeof(Int32)).Invoke(config, new object[] { lambdaEx });

            var prop = Expression.Property(paramEx, "Id");
            var lambdaEx2 = Expression.Lambda(prop, paramEx);
            var ignore = config.GetType().GetMethod("Ignore").MakeGenericMethod(prop.Type);

            var property = config.GetType().GetMethod("Property", new Type[] { lambdaEx.GetType() });

        }

    }}

在这一行

var property = config.GetType().GetMethod("Property", new Type[] { lambdaEx.GetType() });

属性为null,所以我想知道如何调用方法

config.Property(t => t.Id).HasColumnName("ID_CLIENT");

0 个答案:

没有答案