Variable error in Identity

Asked

Viewed 45 times

0

Good afternoon guys, I am supporting a project of my company, which is being used the config Identity.

in this I added your deleted call variable in my database and gave an update-database and updated the database with this new variable, only when I start in my system it displays the following message.

The 'Excluido' property on 'ApplicationUser' could not be set to a 'null' value. You must set this property to a non-null value of type 'System.Boolean'.

below this my identity

using System;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;

namespace IdentitySample.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public string Nome { get; set; }
        public string Cpf { get; set; }
        public Guid RegiaoId { get; set; }
        public bool Excluido { get; set; }
        public string Observacoes { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

        private ApplicationDbContext db = new ApplicationDbContext();
        public async Task<bool> GetByCpfAsync(string cpf)
        {
            var users = db.Users.Where(x => x.Cpf == cpf);
            return await users.AnyAsync();
        }

        public async Task<bool> GetByCpfAsync(string cpf, string id)
        {
            var users = db.Users.Where(x => x.Cpf == cpf && x.Id != id);
            return await users.AnyAsync();
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

            modelBuilder.Entity<IdentityUser>()
                .ToTable("jud_Users");

            modelBuilder.Entity<ApplicationUser>()
                .ToTable("jud_Users");


            modelBuilder.Entity<IdentityUserRole>()
                .ToTable("jud_UserRoles");

            modelBuilder.Entity<IdentityUserLogin>()
                .ToTable("jud_Logins");

            modelBuilder.Entity<IdentityUserClaim>()
                .ToTable("jud_Claims");

            modelBuilder.Entity<IdentityRole>()
                .ToTable("jud_Roles");
        }

        static ApplicationDbContext()
        {
            // Set the database intializer which is run once during application start
            // This seeds the database with admin user credentials and admin role
            Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

Could someone help me?

1 answer

0


By the error message it seems to me that you have not set the value of this new column in the existing records in the database. The framework is probably trying to "hydrate" the entity, but the Excluded property does not accept nulls. Try an "UPDATE TABLE SET Deleted = 0" in the database, or change the type of this property to "bool?".

  • that’s right, I left the variable excluded as "bool?"

  • thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.