简体   繁体   中英

Passing a dbset as a parameter to a function

I couldn't quite find the answer I was looking for searching through stack so Im posting this.

Given this context

  public ProjectionsContext()
        : base("name=ProjectionsDatabase")
    {
    }

    public virtual DbSet<Projection> Projections { get; set; }

    public virtual DbSet<Symbol> Symbols { get; set; }

    public virtual DbSet<TradePriceData> TradePriceDatas { get; set; }

   protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        **//set all decimal properties in Projection Entity to be Required
        var decimalproperties = typeof(Projection).GetProperties()
                                  .Where(p => p.PropertyType == typeof(decimal));
        foreach (var property in decimalproperties)
        {
            var lambda = CreateLambdaExpression<Projection, decimal>(property);
            modelBuilder.Entity<Projection>()
                .Property(lambda)
                .IsRequired();
        }**

        //add the custom configurations for each entity
        modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());

    }
}

I want to refactor out the bolded code, which obviously isn't that difficult but I can't seem to figure out how to pass in an Entity (Projection in the example above) so I can pass in any entity to set the properties.

Something like;

private static void SetEntityPropertiesRequired<TEntity>(
                        DbModelBuilder modelBuilder,
                        TEntity entity)
    {
        //set all decimal properties in Projection Entity to be Required
        var decimalproperties = typeof (entity).GetProperties()
            .Where(p => p.PropertyType == typeof (decimal));

        foreach (var property in decimalproperties)
        {
            var lambda = BuildLambda<entity, decimal>(property);
            modelBuilder.Entity<entity>()
                .Property(lambda)
                .IsRequired();
        }
    }

and calling it like so;

 SetEntityPropertiesRequired<Projection>(modelBuilder);

yields an error, method requires two parameters.

Update:

The answer, per SLaks answer in total is;

  private static void SetEntityPropertiesDecimalTypeToRequired<TEntity>(
                        DbModelBuilder modelBuilder) where TEntity : class
    {
        //set all decimal properties in Projection Entity to be Required
        var decimalproperties = typeof(TEntity).GetProperties()
            .Where(p => p.PropertyType == typeof(decimal));

        foreach (var property in decimalproperties)
        {
            var lambda = BuildLambda<TEntity, decimal>(property);
            modelBuilder.Entity<TEntity>()
                .Property(lambda)
                .IsRequired();
        }
    }

Called like this;

SetEntityPropertiesDecimalTypeToRequired<Projection>(modelBuilder);

A nice bonus would being able to pass in the property type(decimal, int, string, etc)! Anyone want to give that a try since all types arent the same, some are structs and some are classes

You're looking for a generic method:

private static void SetEntityPropertiesRequired<TEntity>(DbModelBuilder modelBuilder)
{
    //set all decimal properties in Projection Entity to be Required
    var decimalproperties = typeof (TEntity).GetProperties()
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM