samedi 26 août 2017

How to run GetMethod.Invoke ? I got error

System.Reflection.TargetException: Object does not match target type.

public class Parameter : BaseEntity
{
    public string Key { get; set; }
    public string Value { get; set; }

    public override void Map(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Parameter>(opt =>
        {
            opt.ToTable("Parameter");
            opt.HasKey(x => x.Id);
            opt.Property(x => x.AutoId).UseSqlServerIdentityColumn();
            opt.HasAlternateKey(x => x.AutoId);
        });
    }
}


public class DataContext<T> : DbContext where T : BaseEntity
{
    private string _connectionString;

    public DataContext(string connectionString)
    {
        _connectionString = connectionString;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        Type t = typeof(T);
        t.GetMethod("Map").Invoke(this, new object[] { modelBuilder }); // this line *****
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseSqlServer(_connectionString);

}

T is called as Parameter class. And (// this line *****) side gives me error.

System.Reflection.TargetException: Object does not match target type.

How can i run this ?





Aucun commentaire:

Enregistrer un commentaire