Correct way of adding (relation) data to a (new) user

I am working on a small application that serves as a “easy to use” way of adding new users. What I want to do is register a new user and assign it to an organisation, which is another table.

The whole thing currently looks like this:

The code (in .NET) currently looks like this:

			BackendlessUser backendlessUser = new BackendlessUser();
			backendlessUser.Email = emailInput.text;
			backendlessUser.SetProperty("name", nameInput.text);

			string name = organisationNames[organisationsDropdown.value];
			BackendlessData.Organisations organisation = organisations.First(o => o.name == name);

			BackendlessData.Organisations org = await Backendless.Data.Of<BackendlessData.Organisations>().FindByIdAsync(organisation.objectId, 2);

			backendlessUser.SetProperty("organisation", org);

			backendlessUser.Password = System.Web.Security.Membership.GeneratePassword(6, 3);

			await Backendless.UserService.RegisterAsync(backendlessUser);

			//int result = Backendless.Data.SetRelation("Users", backendlessUser, "organisation", new object[] { org });

Initially I tried to use the AddProperty of the user class, but that doesn’t seem to add the organisation. The last line was an attempt to add it through the SetRelation call. Although I do get a result back of 1 (which I think indicates that 1 relation value has been set) nothing gets added when I check the console and the organisation field remains empty.

What is the correct way of doing this?

Alright, it seems to be working with the SetRelation call, but I have to do it with the new user that is returned from the RegisterAsync call and I need to refresh the user table to see it has been added.

Hello, @Joey_Fladderak.

Here is an example code:

      dataIdParent_1 = await Backendless.UserService.RegisterAsync( myUser );
      dataIdChildren_1 = await Backendless.Data.Of( "Country" ).SaveAsync( data );
      children = new Object[] { dataIdChildren_1 };
      Backendless.Data.Of( "Users" ).SetRelation( dataIdParent_1, "organisation:Country:1", children );

Best Regards, Nikita.

Thanks @Nikita_Fedorishchev,

That is indeed the way I figured. It was just that I didn’t use the returned user and that I needed to refresh the database before the changes became visible.