创建用户并与实体关联

时间:2015-01-26 14:30:39

标签: c# asp.net-mvc asp.net-identity-2

NET Identity和MVC5,我希望能够扩展Post Action for Register,以便用户可以注册并创建一个类型为Business的新实体,其中包含用户列表的成员,我想要将已创建的用户添加到该列表中。

我尝试了很多方法,而且我无法做到这一点 - 我想我错过了一些基本的东西。

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

以及其他各种方式,例如

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

这有可能吗?

1 个答案:

答案 0 :(得分:0)

根据Andrew Counts和deusExCore的提示 - 我使用了Business实体和用户或ApplicationUser实体之间的一对多关系。正在使用以下命令创建ApplicationUser:

var user = new ApplicationUser {...}

我当时试图创建Business实体的实例并将用户添加到列表中。业务实体根本没有被创建。我没有将用户添加到业务实体,而是将业务实体添加到user.business属性 - 实际上是将用户添加到业务实体的list属性中。

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. 
相关问题