0

NET Identity and also MVC5, I want to be able to extend the Post Action for Register so that it the user can be registered and also create a new entity of type Business which has a member of list of users, I want to add the user that has been created into that list.

I've tried a number of ways, and I'm unable to do this - I think I'm missing something fundamental.

    Business business = new Business {name = "XYZ Pty Ltd"}
business.users.add(user)
db.Businesses.Add(business);
db.savechanges();

and also various other ways, such as after

var result = await UserManager.CreateAsync(user, model.Password);

Is this posible at all?

user3836415
  • 952
  • 1
  • 10
  • 25
  • Each bussiness has only one user or multiple? Can a user be part of multiple bussiness? – Sefa Jan 26 '15 at 14:34
  • 1
    this should work if you have a user and there is a user navigation property on the business.... are you getting an error or having some other issue? – Claies Jan 26 '15 at 14:35
  • 2
    Can you clarify what you have actually tried and what is giving you trouble? Usually, a question that ends with "Is this possible..." is suggesting that you haven't actually tried a solution, but are hoping someone else will give you working code. – Claies Jan 26 '15 at 14:43
  • hi Andrew and deusExCore, thanks for the hints. I realised what I was doing wrong and what I couldn't quite understand. I was doing business.users.add(user); instead of doing user.business = business; I wasn't able to save the business entity. I've altered the code and it is now working. Thanks – user3836415 Jan 27 '15 at 02:21

1 Answers1

0

Based on the hints from Andrew Counts and deusExCore - I was using a one-to-many relationship between Business entity and users or ApplicationUser entity. The ApplicationUser is being created using :

var user = new ApplicationUser {...}

I was trying to then create an instance of the Business entity and add the user to the list. The business entity was simply not getting created. Instead of adding the user to the business entity, I added the business entity to the user.business property - which is essentially adding the user to the list property of the business entity.

e.g:

var user = new ApplicationUser {...}
Business business = new Business {...}
user.business = business;

... // rest of the standard code form MS for registration of the user, this will then store the business entity and establish the relationships between the two entities. 
user3836415
  • 952
  • 1
  • 10
  • 25