add new column to database table dynamically

☆樱花仙子☆ 提交于 2020-01-06 02:30:09

问题


As EF Migration do provide a way to create new columns as part of migration like below

public class AddRatingMig : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.Movies", "Rating", c => c.String());
    }

    public override void Down()
    {
        DropColumn("dbo.Movies", "Rating");
    }
}

But I want to create a column as part of code and not thru migration. We are providing admins an option to capture more information about the users of the application. The facility to do that will be thru some checkboxes on front end. On the basis of that, columns will be added on backend.

So what I am looking at is, an injectionor for migration-implementor, using which i can call the "AddColumn" method. I can hardcode it, but given that EF-migration or fluent-migrator does that using a fluent syntax, was checking if I could use it.

        public interface IDbManagerService {
             void AddColumn(string table, string column)
        }


        public class DbManager: IDbManagerService {
             private readonly ??? dbMigrator;
             void AddColumn(string table, string column) {
                  dbMigrator.AddColumn(table, column)
             }
        }

来源:https://stackoverflow.com/questions/58966669/add-new-column-to-database-table-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!