I have a Web API 2 project using MVC. It uses entity framework. This entity framework uses a database first approach with a .edmx file.

The project is based on VS 2013 Express Web API 2 template. I just used my own database. I didn't modify any account related code. But when I try to register a new user, the following statement in AccountController.cs throw exception:

public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
    ...
    IdentityResult result = await UserManager.CreateAsync(user, model.Password);
    ...
}

The exception says:

Cannot insert the value NULL into column 'Discriminator', table 'xxx.dbo.AspNetUsers'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Can anyone help me? Thank you!

有帮助吗?

解决方案 2

I upgraded all NuGet Packages yesterday. Among them Asp.net Identity is upgraded to 2.0.0.0.

It magically worked.

So I suspect that was because of some bug in Identity 1.0

其他提示

The answer is in your question's body.

table 'xxx.dbo.AspNetUsers'; column does not allow nulls.

Seems to be, that you're trying to insert a NULL value from your model instance RegisterBindingModel model, where the structure of your table in database server doesn't allow to accept NULL value.

Try to debug and check, is your model instance is creating correctly when the request is coming.

Also I can see, that you use:

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

Why user isn't a member of your model, like the Password property? Maybe try to use:

IdentityResult result = await UserManager.CreateAsync(model.User, model.Password);

Anyway... I don't have more info and don't imagine, what is inside your model instance, how you're binding data in it from the request. You should to look in the debugger and debug very well to repair your project or provide more info.

But one thing is clear as a crystal, you're trying to insert a NULL value into the column, which doesn't accept NULL, so you must check how is your model binding. Maybe your client-side application doesn't send correctly some arguments, maybe you're binding you model incorrectly, maybe something else... There is a need to get more information from you to help you, otherwise you should debug carefully.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top