Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Foreign key constraint TenantId #340


User avatar
0
nanouz created

Hi,

I'm trying to make a Register/Login screen. I started with the boilerplate template and added the module zero packages. I don't have to work with multiple tenants. I got this code for register :

    [HttpPost]
    public async Task<ActionResult> Register(RegisterModel model)
    {
        var user = new User
        {
            Name = model.FirstName,
            Surname = model.LastName,
            UserName = model.Email,
            EmailAddress = model.Email,
            Password = new PasswordHasher().HashPassword(model.Password),
            IsActive = true,
            Roles = new List<UserRole>()
        };
        
        user.Roles.Add(new UserRole { RoleId = 3 }); // Role: Researcher

        await _userManager.CreateAsync(user);

        return RedirectToAction("Login");
    }

Whenever I try to register I get the error: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AbpUsers_dbo.AbpTenants_TenantId". The conflict occurred in database "Agrolink", table "dbo.AbpTenants", column 'Id'. The statement has been terminated.

I tried added a default Tenant in the database and added TenantId in the User object but I get the same error. I don't understand tenants really and no idea why I this error pops up.

Any help is much appreciated!


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Why don't you use "ABP + module zero" template. it has a login page. Also, I'm adding a regisster page, social logins and token based auth in next version (will be released in a few days).

    Normally, module-zero assumes there is a 'Default' tenant with Id = 1 for a single tenant app. All users should have TenantId = 1 for single tenant apps. I couldn't understand why it does not save with TenantId = 1. You may check SQL Profiler for generated SQL, try it manually and find the error. It does not seems related to tenancy.