Created
May 27, 2016 18:28
-
-
Save drmcarvalho/2492e1bc95683482b5e5ff0a5ce29deb to your computer and use it in GitHub Desktop.
Modelo Categoria
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------ | |
// <auto-generated> | |
// This code was generated from a template. | |
// | |
// Manual changes to this file may cause unexpected behavior in your application. | |
// Manual changes to this file will be overwritten if the code is regenerated. | |
// </auto-generated> | |
//------------------------------------------------------------------------------ | |
namespace LogixMine | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
public partial class CategoriaProduto : IValidatableObject | |
{ | |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] | |
public CategoriaProduto() | |
{ | |
this.SubCategoria = new HashSet<SubCategoria>(); | |
this.Produto = new HashSet<Produto>(); | |
} | |
public int id_categoria { get; set; } | |
public string descricao { get; set; } | |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] | |
public virtual ICollection<SubCategoria> SubCategoria { get; set; } | |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] | |
public virtual ICollection<Produto> Produto { get; set; } | |
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | |
{ | |
if (string.IsNullOrEmpty(this.descricao)) | |
{ | |
yield return new ValidationResult("A descrição da categoria é obrigatória.", new[] { "descricao" }); | |
} | |
if (mesmaDescricao(this.descricao)) | |
{ | |
yield return new ValidationResult("Já existe uma categoria com o mesmo nome cadastrada.", new[] { "descricao" }); | |
} | |
} | |
private bool mesmaDescricao(string descricao) | |
{ | |
if (this.descricao != this.descricao) | |
{ | |
using (LOGIXMINEEntities context = new LOGIXMINEEntities()) | |
{ | |
if (context.CategoriaProduto.Any(x => x.descricao.Contains(this.descricao))) | |
{ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment