简体   繁体   English

动态创建表达式

[英]Create expression dynamically

Using FluentMigrator when i want to insert data into table i must use syntax like that:当我想将数据插入表时使用FluentMigrator ,我必须使用如下语法:

Insert.IntoTable(table).InSchema(shcema).Row().Row(obj1).Row(obj2).Row(obj3)... // and so on

My question is, how we can build that ...Row()... expression dynamically?我的问题是,我们如何动态构建...Row()...表达式?

Basicly i want to mock some data in loop.基本上我想在循环中模拟一些数据。 How to add that .Row() methods dynamically to expression?如何将.Row()方法动态添加到表达式中?

InSchema() method declaration looks like this: InSchema()方法声明如下所示:

public interface IInsertDataOrInSchemaSyntax : IInsertDataSyntax
    {
        /// <summary>
        /// Specify the schema of the table to insert data
        /// </summary>
        /// <param name="schemaName">The schema</param>
        /// <returns>The next step</returns>
        IInsertDataSyntax InSchema(string schemaName);
    }

And for Row() :对于Row()

public interface IInsertDataSyntax : IFluentSyntax
    {
        /// <summary>
        /// Specify the data to insert
        /// </summary>
        /// <param name="dataAsAnonymousType">An anonymous object that is used to insert data</param>
        /// <remarks>
        /// The properties are the column names and their values are the row values.
        /// </remarks>
        /// <returns>The next step</returns>
        IInsertDataSyntax Row(object dataAsAnonymousType);

        /// <summary>
        /// Specify the data to insert
        /// </summary>
        /// <param name="data">The dictionary containing column name/value combinations</param>
        /// <returns>The next step</returns>
        IInsertDataSyntax Row(IDictionary<string, object> data);
    }

Could you just call Insert()... in a loop?您可以循环调用 Insert()... 吗? This is what I do for inserting fake users, companies, etc.这就是我插入虚假用户、公司等的方法。

for (int i = 0; i < 20; i++)
{
    Insert.IntoTable("Company").InSchema("Customer").Row(new
    {
        CompanyName = $"Company {i}",
        // Other columns...
    });
}

You may want to look into the package Faker.Net, which will generate useful information like names, addresses, phone numbers etc.您可能想查看 package Faker.Net,它会生成有用的信息,如姓名、地址、电话号码等。

for (int i = 0; i < 20; i++)
{
    Insert.IntoTable("Company").InSchema("Customer").Row(new
    {
        CompanyName = Faker.Company.Name(),
        // Other columns...
    });
}

I think this version should work.我认为这个版本应该可以工作。

            var customers = new List<object> { new { name = "Ed" }, new { name = "Edd" }, new { name = "Eddy" } };
            var insertDataSyntax = Insert.IntoTable("customers").InSchema("schemaName");
            customers.ForEach(c => insertDataSyntax.Row(c));

or this:或这个:

            var customers = new object[] { new { name = "Ed" }, new { name = "Edd" }, new { name = "Eddy" } };
            var insertDataSyntax = Insert.IntoTable("customers").InSchema("schemaName");
            customers.Aggregate(insertDataSyntax, (current, customer) => current.Row(customer));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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