3
The MVC of my project is stuccoed in services, controllers, Views and models.
My deletion screen works, but when the record is used in other tables it displays this error.
The DELETE statement conflicted with the REFERENCE constraint "FK_XYZ". The conflict occurred in database "ETEST", table "dbo.TEST", column 'ID_TEST'.
The statement has been terminated.
This is because the table’s PK is FK in other tables.
I would like that before deleting, the system would make a check if the record is used in the bank. To show a message to the friendly user before trying to delete. Ex.: Registration cannot be deleted because it is in use.
How do I build this validation?
Model
public int Id {get; set;}
[Display(Name = "Codigo", ResourceType = typeof(Resources.TestResources))]
[Required]
public string Codigo { get; set; }
[Display(Name = "Descricao", ResourceType = typeof(Resources.TestResources))]
[Required]
public string Descricao { get; set; }
[Display(Name = "UsuarioAnalistaCusto", ResourceType = typeof(Resources.TestResources))]
[Required]
public string UsuarioAnalistaCusto { get; set; }
Controller
public void Excluir(int id)
{
this.Service.Delete(id);
}
Services
public void Delete(int id)
{
var item = this.context.Test.Find(id);
this.context.Test.Remove(item);
base.Save();
}
Context
namespace TXT.Test.eTest.DataAccess
{
public partial class EContext : DbContext
{
static EContext()
{
Database.SetInitializer<ECContext>(null);
}
public EContext(bool proxyCreationEnabled = true)
: base("Name=EContext")
{
base.Database.CommandTimeout = 60 * 5; // TOOO: utilizar configuration
base.Configuration.ProxyCreationEnabled = proxyCreationEnabled;
base.Configuration.AutoDetectChangesEnabled = false;
}
public DbSet<Empresa> Empresas { get; set; }
public DbSet<Fornecedor> Fornecedores { get; set; }
public DbSet<Desenho> Desenhos { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new EMPRESAMap());
modelBuilder.Configurations.Add(new FORNECEDORMap());
modelBuilder.Configurations.Add(new DESENHOMap());
...
}
...
}
}
If you do not use EF, you have to use your repository default. How is this access to the database?
– Leonel Sanches da Silva
the Database is mapped, within a class that calls Dataaccess
– Thamires Cunha
But it uses context. Does this Dataaccess simulate a context? How is this?
– Leonel Sanches da Silva
I posted an example of how it’s done for you to see
– Thamires Cunha
So. Use Entity Framework Yes. Context derives from
DbContext
. In this case just check if the parent browsing property is not null, but still considering that you disable the dynamic proxy, this can get more complicated. You need an answer?– Leonel Sanches da Silva
hm.. I understand. Would it be easier to make an error handling for the user, instead of the validation?. R= This record cannot be deleted as it is being used.
– Thamires Cunha
That may be. I’ll suggest something.
– Leonel Sanches da Silva
It’s not necessary to put technologies in the title - that’s what tags are for.
– dcastro