Pages

Wednesday, January 14, 2015

DataAnnotations Cheat Sheet (esp)

He buscado por todo lado un Cheat Sheet como este para tener todas las opciones a mano pero no pude encontrar nada. Asi que arme uno propio, espero que les sirva de ayuda:


DataAnnotations Cheat Sheet v1.0


Nombre: AssociationAttribute

Descripcion: Especifica que un miembro de la entidad representa una relación, como una relacion de llave foranea.
Uso: [Association(Name, OtherKey, ThisKey)]
Ejemplo:
[Table(Name = "Clientes")]
public partial class Cliente
{
    [Column(IsPrimaryKey = true)]
    public string ClienteID;
    // ...
    private EntitySet<Pedido> _Pedidos;
    [Association(Storage = "_Pedidos", OtherKey = "ClienteID")]
    public EntitySet<Pedido> Pedidos

    {
        get { return this._Pedidos; }
        set { this._Pedidos.Assign(value); }
    }
}

Nombre: BindableTypeAttribute
Descripcion: Especifica si es que un tipo es tipicamente usado para hacer un   binding.
Uso: [BindableType(IsBindable = bool)]
Ejemplo:
  [BindableType(IsBindable = false)]
  public enum EstadoDeEntidad
  {
    Detached = 1,
    SinCambios = 2,
    Adicionado = 4,
    Borrado = 8,
    Modificado= 16,
  }


Copiar filas en la misma tabla solo cambiando el Id (TSQL) (esp)

Vamos a analizar diferentes escenarios en los cuales necesitamos copiar/clonar filas de una tabla a esa misma tabla, Solo cambiando el valor de un campo, Que en este caso sera el campo Id, Usando Microsoft SQL Server.

Supongamos que tenemos el AntId (Id original Id de el que queremos copiar las filas, en este caso este Id puede estar en varias filas o solo en una) y el NuevoId ( El Id nuevo que queremos poner en las filas que vamos a copiar/clonar)

Caso 1 - conocemos el nombre de la tabla y de todos sus campos:

El primer caso es de una tabla de la cual sabemos de antemano su nombre y su estructura, así que podemos escribir una consulta como la siguiente:

DECLARE @OldId int
DECLARE @NuevoId int
SET @AntId =13456 -- ejem.
SET @NuevoId =45687 -- ejem.

INSERT INTO MiTabla(Id,ColumnaA,ColumnaB,ColumnaC)
SELECT @NuevoId ,ColumnaA,ColumnaB,ColumnaC
FROM MyTable WHERE Id=@AntId 


Copying Rows On The Same Table and updating Only some fields (TSQL)

We will analyze different scenarios on which we need to copy or clone rows from one table to the same table, only changing the value of one know field, which in this case is the Id field, Using Microsoft SQL Server.

Supposing we are provided the OldId (original Id from were we want to copy the rows, in this case this Id can be on several rows or just one) and a NewId ( the Id we want to put on the row we are going to copy/clone)


If we know the columns and table names before hand:

The first scenario would be a table on which we know before hand the name of the table and all the fields involved, so we can easily write the following query.

DECLARE @OldId int
DECLARE @NewId int
SET @OldId =13456 -- i.e
SET @NewId =45687 -- i.e

INSERT INTO MyTable(Id,ColumnA,ColumnB,ColumnC)
SELECT @NewId,ColumnA,ColumnB,ColumnC
FROM MyTable WHERE Id=@OldId


Wednesday, May 7, 2014

Entity Framework DataAnnotations Cheat Sheet

I have been looking everywhere for a Cheat Sheet like this but could not find anything, So I had to make my own, I hope you find it useful:


DataAnnotations Cheat Sheet v1.0


Name: AssociationAttribute

Description: Specifies that an entity member represents a data relationship, such as a foreign key relationship.
Usage: [Association(Name, OtherKey, ThisKey)]
Example:
[Table(Name = "Customers")]
public partial class Customer
{
    [Column(IsPrimaryKey = true)]
    public string CustomerID;
    // ...
    private EntitySet<Order> _Orders;
    [Association(Storage = "_Orders", OtherKey = "CustomerID")]
    public EntitySet<Order> Orders
    {
        get { return this._Orders; }
        set { this._Orders.Assign(value); }
    }
}

Name: BindableTypeAttribute
Description: Specifies whether a type is typically used for binding.
Usage: [BindableType(IsBindable = bool)]
Example:
  [BindableType(IsBindable = false)]
  public enum EntityState
  {
    Detached = 1,
    Unchanged = 2,
    Added = 4,
    Deleted = 8,
    Modified = 16,
  }

Monday, September 16, 2013

CodeIgniter's Pagination with a Search Term

On this article I’ll try to explain how to use CodeIgniter’s pagination library with a search term in a way that that our links look like:

Controller\action\search_term\page

CodeIgniter’s pagination library makes it easy for us to show long data lists generating tha links for the corresponding pages automatically, But it lacks a way to include a search term on the paginated results.

The problem is that if we inlcude a search term with the pagination, there is no specific function to include this search teram on the links for the following pages. As an extra complication we will solve this problem including a subset of data, on which we will apply both the pagination and an optional search term. And getting the links like this:

Controller\accion\id\termino de búsqueda\pagina.

We have the following scenario:

 - A list of cities.
 - A list of people who belong to a city.
 - We want to list the people belonging to a determined city, and we want to paginate the results, but also we want to be able to search in this result set and have a paginated result, using the same view.