This project has moved. For the latest updates, please go here.

DB Code Generation

Apr 30, 2013 at 12:45 PM
Hello

The DB Code for database manipulation is done using stored procedure. How Can I generate the LINQ code from CSLAGenFork.

For example the code is generated like this for my appointment class.

public AppointmentsDto Insert(AppointmentsDto appointments)
    {
        using (var ctx = ObjectContextManager<SchedulerEntities>.GetManager("MyDatabase"))
        {
            using (var cmd = new SqlCommand("dbo.AddAppointments", ctx.Connection))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@business_location_id", appointments.Business_location_id).DbType = DbType.Int32;
                cmd.Parameters.AddWithValue("@appointment_id", appointments.Appointment_id).Direction = ParameterDirection.Output;
                cmd.Parameters.AddWithValue("@start_time", appointments.Start_time.DBValue).DbType = DbType.DateTime;
                cmd.Parameters.AddWithValue("@duration", appointments.Duration == null ? (object)DBNull.Value : appointments.Duration.Value).DbType = DbType.Int16;
                cmd.Parameters.AddWithValue("@end_time", appointments.End_time.DBValue).DbType = DbType.DateTime;
                cmd.Parameters.AddWithValue("@patient_id", appointments.Patient_id).DbType = DbType.Int32;
                cmd.Parameters.AddWithValue("@employee_id", appointments.Employee_id).DbType = DbType.Int32;
                cmd.Parameters.AddWithValue("@confirmation_dt", appointments.Confirmation_dt.DBValue).DbType = DbType.DateTime;
                cmd.Parameters.AddWithValue("@service", appointments.Service == null ? (object)DBNull.Value : appointments.Service).DbType = DbType.String;
                cmd.Parameters.AddWithValue("@status", appointments.Status == null ? (object)DBNull.Value : appointments.Status).DbType = DbType.String;
                cmd.Parameters.AddWithValue("@show_flag", appointments.Show_flag == null ? (object)DBNull.Value : appointments.Show_flag).DbType = DbType.String;
                cmd.Parameters.AddWithValue("@confirm_source", appointments.Confirm_source == null ? (object)DBNull.Value : appointments.Confirm_source).DbType = DbType.String;
                cmd.Parameters.AddWithValue("@all_day_flag", appointments.All_day_flag == null ? (object)DBNull.Value : appointments.All_day_flag).DbType = DbType.String;
                cmd.Parameters.AddWithValue("@notes", appointments.Notes == null ? (object)DBNull.Value : appointments.Notes).DbType = DbType.String;
                cmd.Parameters.AddWithValue("@insurance", appointments.Insurance == null ? (object)DBNull.Value : appointments.Insurance).DbType = DbType.String;
                cmd.Parameters.AddWithValue("@resource_id", appointments.Resource_id == null ? (object)DBNull.Value : appointments.Resource_id.Value).DbType = DbType.Int32;
                cmd.Parameters.AddWithValue("@preappointment_flag", appointments.Preappointment_flag == null ? (object)DBNull.Value : appointments.Preappointment_flag).DbType = DbType.String;
                cmd.Parameters.AddWithValue("@created_source", appointments.Created_source == null ? (object)DBNull.Value : appointments.Created_source).DbType = DbType.String;
                cmd.Parameters.AddWithValue("@created_dt", appointments.Created_dt.DBValue).DbType = DbType.DateTime;
                cmd.Parameters.AddWithValue("@color_code", appointments.Color_code == null ? (object)DBNull.Value : appointments.Color_code.Value).DbType = DbType.Int32;
                cmd.Parameters.AddWithValue("@appointment_metadata", appointments.Appointment_metadata == null ? (object)DBNull.Value : appointments.Appointment_metadata).DbType = DbType.String;
                cmd.Parameters.AddWithValue("@category", appointments.Category == null ? (object)DBNull.Value : appointments.Category).DbType = DbType.String;
                cmd.ExecuteNonQuery();
                appointments.Appointment_id = (int)cmd.Parameters["@appointment_id"].Value;
            }
        }
        return appointments;
    }
But I want it should generate something like this

public void Insert(AppointmentDto dto)
    {
        using (var ctx = DbContextManager<ExampleDatabaseEntities>.GetManager())
        {
            //var currentDate = ctx.DbContext.CreateQuery<DateTime>("CurrentUtcDateTime()").AsEnumerable().First();

            var newItem = new Appointment
            {
                UniqueID = dto.Id,
                StartDate = dto.Start,
                EndDate = dto.End,
                ConfirmationDate = dto.ConfirmationDate,
                Status = (int)dto.Status,
                PatientId = dto.PatientId,
                PhysicianId = dto.PhysicianId,
                ServiceId = dto.ServiceId,
                Description = dto.Description,
                Label = (int)dto.Label,
                LocationId = dto.LocationId,
                AllDay = dto.AllDay,
                Subject = dto.Subject,
                Type = (int)dto.EventType,
                RecurrenceInfo = dto.RecurrenceInfo,
                ReminderInfo = dto.ReminderInfo
            };

            ctx.DbContext.Appointments.Add(newItem);
            ctx.DbContext.SaveChanges();
            dto.LastChanged = newItem.LastChanged;
        }
    }
Is the above mentioned stuff possible and if Yes please mention how?

Thanks in advance,
Ronak
May 1, 2013 at 12:03 PM
Please respond to my query.

Thanks,
Ronak
Coordinator
May 1, 2013 at 9:37 PM
Edited May 1, 2013 at 9:39 PM
Hi Ronak,

CslaGenFork supports only ADO.NET. Linq to SQL is deprecated by Microsoft and Entity Framework code generation wouldn't make sense, since Entity Framework itself generates code. I don't know if that answers your question as I didn't understand it. I understand and agree inline queries generation is a must, but I don't understand your point.

My question is:
  • Does the generated code works? If yes, why do you want CslaGenFork to generate something different?
If you think CslaGenFork adds value to your tool set but you don't like the data access code, you can always tell CslaGenFork not to generate the data access code (set 00. Generate Options => Generate Data Access Region to False).

Regards
Tiago Freitas Leal