0

Boa tarde pessoal, estou dando suporte a um projeto da minha empresa, que esta sendo utilizado o identity config.

nisto eu adicionei ua variavel chamada excluido no meu banco de dados e dei um update-database e atualizou o banco com esta nova variavel, só que quando eu dou start no meu sistema ele exibe a seguinte mensagem.

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'.

abaixo esta meu 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();
        }
    }
}

Alguém poderia me ajudar?

1 Resposta 1

0

Pela mensagem de erro me parece que você não definiu o valor dessa nova coluna nos registros existentes no banco de dados. Provavelmente o framework está tentando "hidratar" a entidade, mas a propriedade Excluido não aceita nulos. Tente um "UPDATE TABELA SET Excluido = 0" no banco de dados, ou altere o tipo dessa pripriedade para "bool?".

2
  • isso mesmo, deixei a variavel excluido como "bool?" 9/10/2017 às 19:55
  • muito obrigado! 9/10/2017 às 19:55

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .