Возникло исключение: Microsoft.EntityFrameworkCore.DbUpdateException в Microsoft.EntityFrameworkCore.dll - PullRequest
0 голосов
/ 07 августа 2020

Я использую службу аутентификации с ASP NET Core MVC API

Я создал эту базу данных в SSMS: SSMS DB

In Visual Studio this is my User class:

using System;
using System.Collections.Generic;

namespace AuctionAdventureAuth.Models
{
    public partial class User
    {
        public string Username { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
    }
}

Then i created the RegisterController like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AuctionAdventureAuth.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

// For more information on enabling Web API for empty projects, visit 
https://go.microsoft.com/fwlink/?LinkID=397860

namespace AuctionAdventureAuth.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RegisterController : ControllerBase
    {
        AuctionAdventureAuthContext dbContext = new AuctionAdventureAuthContext();   
        
        // POST api/
        [HttpPost]
        public string Post([FromBody] User value)
        {
            //check if user exists in database
            if (!dbContext.User.Any(user => user.Username.Equals(value.Username))){
                User user = new User();
                user.Username = value.Username;
                user.Email = value.Email;
                user.Password = value.Password;

                try
                {
                    dbContext.Add(user);
                    dbContext.SaveChanges();
                    return JsonConvert.SerializeObject("Registed Succesfully");
                }
                catch (Exception ex) {
                    return JsonConvert.SerializeObject(ex.Message);
                }
            }
            else {
                return JsonConvert.SerializeObject("User already exists in Database");
            }
        }
    }
}

Running the API, this is my cmd:

Использование расширения Advanced REST Client Chrome и попытка протестировать метод POST в

RequestUrl: http://localhost: 5000 / api / register

Я получаю следующее исключение:

Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in 
Microsoft.EntityFrameworkCore.dll

Что это может быть?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...