Can I get the record after Linq InsertRow() using C#

I use InsertRow() method to insert a new line to mysql table, the record id of this table is generated by a trigger, can I get the new id after ApplyChanges() without query this table again?

Hello

If the field you mention is not a Primary Key field then it is enough to just enable the ‘Server Autorefresh’ option for the field. In this case the server side will autorefres the field value and will send this value back to the client.

However there is an issue with the trigger-generated PK value: how application that inserts the data can find out the new Id of that data? Iow the data row is inserted into the databse, but the server application that inserted it doesn’t know its Id so it cannot provide this Id to the client app.

The solution here is to use a Business Processor to pre-calculate the Id field value. Move out the code that generates the Id into a SQL function and then do something like what happens in this code:

	private void CustomersBusinessProcessor_BeforeProcessChange(RemObjects.DataAbstract.Server.BusinessProcessor sender, RemObjects.DataAbstract.Server.DeltaChangeEventArgs e)
	{
		if (e.DeltaChange.Type != ChangeType.Insert)
		{
			return;
		}

		e.DeltaChange.NewValues["Id"] = "BL_" + GetGeneratorValue("aa");
	}

	private int GetGeneratorValue(string name)
	{
		using (var generatorFetchCommand = this.Connection.NewCommand(functionCallSql, System.Data.CommandType.Text))
		{
			return Convert.ToInt32(generatorFetchCommand.ExecuteScalar()); // Change to Convert.ToInt64 depending on your numbers range
		}
	}

Here CustomersBusinessProcessor_BeforeProcessChange is an event handler attached to a business processor. In its code a value of the Id field is calculated (don’t forget to enable the ‘Server Autorefresh’ option for this field).

Regards