No Auto-Increment Primary Key for CPF in Entity Framework

Asked

Viewed 662 times

4

The scenario is as follows: I have an Employee class. A primary key of it must be the employee CPF that the user type.

The Problem: the key follows a default auto increment, not going according to the entered CPF.

EX: I typed CPF official: 492.203.120-90 if it is the first; When the record of the table persists, the key will be inserted with the value 1.

Working class:

public class Funcionario
{
    [Key]
    [Display(Name="CPF: ")]
    [Range(0,long.MaxValue)]
    [Cpf(ErrorMessage = "Valor Inválido para CPF!")]
    public long  FUN_CPF { get; set; }

    [Required(ErrorMessage = "Digite o nome do funcionário!")]
    [MinLength(5, ErrorMessage = "O tamanho mínimo do nome são 5 caracteres.")]
    [StringLength(64, ErrorMessage = "O tamanho máximo são 64 caracteres.")]
    [Display(Name = "Nome: ")]
    public string FUN_NOME { get; set; }

    [Display(Name = "Data de Nascimento: ")]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
    public DateTime FUN_DTNASCIMENTO { get; set; }

    [Display(Name = "Cargo: ")]
    [MinLength(5, ErrorMessage = "O tamanho mínimo do nome são 5 caracteres.")]
    [StringLength(64, ErrorMessage = "O tamanho máximo são 64 caracteres.")]
    public string FUN_CARGO { get; set; }

    [Display(Name = "Data de Adminssão: ")]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
    public DateTime FUN_DTADMISSAO { get; set; }

    [Display(Name = "Data de Demissão: ")]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
    public DateTime? FUN_DTDEMISSAO { get; set; }

    //Data pós demissão desativar funcionario
    [Display(Name = "Ativo: ")]
    public bool FUN_ATIVO { get; set; }

}

Methodology for CRUD is as follows :

  public interface IGenericRepository<TEntity>
{

    Task<TEntity> GetByIdAsync(int id);

    IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate);

    IQueryable<TEntity> GetAll();

    Task EditAsync(TEntity entity);

    Task InsertAsync(TEntity entity);

    Task DeleteAsync(TEntity entity);

   // Task Dispose(TEntity entity);

}

The implementation:

   public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    protected DbSet<TEntity> DbSet;

    private readonly DbContext _dbContext;

    public GenericRepository(DbContext dbContext)
    {
        _dbContext = dbContext;
        DbSet = _dbContext.Set<TEntity>();
    }

    public GenericRepository()
    {
    }

    public IQueryable<TEntity> GetAll()
    {
        return DbSet;
    }

    public async Task<TEntity> GetByIdAsync(int id)
    {
        return await DbSet.FindAsync(id);
    }

    public IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate)
    {
        return DbSet.Where(predicate);
    }

    public async Task EditAsync(TEntity entity)
    {
        _dbContext.Entry(entity).State = EntityState.Modified;
        await _dbContext.SaveChangesAsync();
    }

    public async Task InsertAsync(TEntity entity)
    {

        DbSet.Add(entity);
        await _dbContext.SaveChangesAsync();
    }

    public async Task DeleteAsync(TEntity entity)
    {
        _dbContext.Entry(entity).State = EntityState.Modified;
        await _dbContext.SaveChangesAsync();
    }

1 answer

2


The idea of using CPF as a primary key can be quite interesting within your system. Just decorate your key with [DatabaseGenerated(DatabaseGeneratedOption.None)] for the system to define the primary key, not the database:

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Name="CPF: ")]
[Range(0,long.MaxValue)]
[Cpf(ErrorMessage = "Valor Inválido para CPF!")]
public long  FUN_CPF { get; set; }

Browser other questions tagged

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