I'm using DAPPER with JSON and C # where I'm looking to update 2 tables of a database made in MYSQL: PERSON and MASTER ... Then I leave the fields of both objects in C #, the setting of the JSONs that I use to insert, the fields of the tables and the stored procedure that you insert:
C #
public enum ESexo
{
Masculino,
Femenino
}
public class Persona
{
public int Id { get; set; }
public string Nombre { get; set; }
public string Apellidos { get; set; }
public DateTime FechaNacimiento { get; set; }
public ESexo Sexo { get; set; }
public bool Status { get; set; }
}
public class Maestro : Persona
{
public int IdPersona { get; set; }
public string Folio { get; set; }
public string Asignatura { get; set; }
public decimal Salario { get; set; }
}
JSON
{
"nombre": "Sofia",
"apellidos": "Medina Ponce",
"fechaNacimiento": "1987-10-11",
"sexo": 1,
"folio": "MED10587",
"asignatura":"Ingles",
"salario" : "14500"
}
MYSQL DATABASE TABLES
CREATE TABLE 'tbl_maestro' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'persona_id' int(11) NOT NULL DEFAULT '0',
'folio' varchar(45) NOT NULL DEFAULT '"',
'asignatura' varchar(45) NOT NULL DEFAULT '"',
'salario' decimal(11,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY ('id'),
KEY 'fk_maestro_persona_idx' ('persona_id'),
CONSTRAINT 'fk_maestro_persona' FOREIGN KEY ('persona_id') REFERENCES 'tbl_persona' ('id') ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
CREATE TABLE 'tbl_persona' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'nombre' varchar(45) NOT NULL,
'apellidos' varchar(100) NOT NULL,
'sexo' bit(1) NOT NULL,
'status' bit(1) NOT NULL,
'fecha_nacimiento' date NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
PROCEDURES STORED IN MYSQL
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE 'stp_persona_insertar'(
out p_id int,
in p_nombre varchar(45),
in p_apellidos varchar(100),
in p_sexo bit,
in p_fecha_nacimiento date
)
BEGIN
INSERT INTO 'bd_colegio'.'tbl_persona'
(
'nombre',
'apellidos',
'sexo',
'fecha_nacimiento',
'status')
VALUES
(
p_nombre,
p_apellidos,
p_sexo,
p_fecha_nacimiento,
1);
SET p_id = LAST_INSERT_ID();
END$$
DELIMITER ;
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE 'stp_maestro_insertar'(
out p_id int,
out p_persona_id int,
in p_nombre varchar(45),
in p_apellidos varchar(100),
in p_sexo bit,
in p_fecha_nacimiento date,
in p_folio varchar(45),
in p_asignatura varchar(45),
in p_salario decimal(11,2)
)
BEGIN
CALL 'bd_colegio'.'stp_persona_insertar'(
p_persona_id,
p_nombre,
p_apellidos,
p_fecha_nacimiento,
p_sexo);
INSERT INTO 'bd_colegio'.'tbl_maestro'
(
'persona_id',
'folio',
'asignatura',
'salario')
VALUES
(
p_persona_id,
p_folio,
p_asignatura,
p_salario);
SET p_id = LAST_INSERT_ID();
END$$
DELIMITER ;
PROCEDURE STORED IN C #
public static Maestro Insertar(Maestro maestro)
{
using (var conexion = new MySqlConnection(CadenaConexion))
{
var parametros = new DynamicParameters();
parametros.Add("p_id", dbType: DbType.Int32, direction: ParameterDirection.Output);
parametros.Add("p_persona_id", dbType: DbType.Int32, direction: ParameterDirection.Output);
parametros.Add("p_nombre", maestro.Nombre);
parametros.Add("p_apellidos", maestro.Apellidos);
parametros.Add("p_fecha_nacimiento", maestro.FechaNacimiento);
parametros.Add("p_sexo", maestro.Sexo);
parametros.Add("p_folio", maestro.Folio);
parametros.Add("p_asignatura",maestro.Asignatura);
parametros.Add("p_salario", maestro.Salario);
conexion.Execute("stp_maestro_insertar", parametros, commandType: CommandType.StoredProcedure);
var idMaestro = parametros.Get<int>("p_id");
return ObtenerMaestroPorId(idMaestro);
}
}
PD. The thing is that when I use that stored procedure I receive the following error: {"Error parsing column 6 (Birthdate = 1 - UInt64) "} And it inserts me in both tables but the part of the date leaves it empty ... Thank you very much