The following code reads from Microsoft EntityFramework 6.0 domain model and returns the single view_patient object. The view_patient object is defined in the backend database and uploaded to the .edmx model of EF.
I am new to EF, but it seems there should be a better way--(better means less typing)-- of going from model to object without the intermediary anonymous data transfer object. As I have a couple hundred of these procedures to write (all within a WCF service), is there a better way of doing this? Or perhaps a "generic" method that will allow going directly from Link-to-entity to output of objects?
A note towards naming convention: The classes are coming directly from Npgsql 3.0.5 EntityFramework interface -- which in turn is being read directly from PostgreSQL 9.5 database. I can not seem to get either EF or Npgsql to return PascalCase from the PostgreSQL database which is case-insensitive and, without adding quotes to all names, returns only lowercase names for tables, stored procedures, and columns.
Note #2: When not going through the intermediate DTO, I get the following error:
The entity or complex type 'chaosModel.view_patient' cannot be constructed in a LINQ to Entities query.
(Using .NET 4.5)
// testing the setup
public view_patient Test(string cpatient)
{
using (var ctx = new chaosEntities())
{
var q = (from patient in ctx.patients
join chart in ctx.charts on patient.chart_recid equals chart.recid
where patient.cpatient == cpatient
select new
{
birthdate = patient.birthdate,
chart_number = chart.chart_number,
chart_recid = chart.recid,
city = patient.city,
cpatient = patient.cpatient,
donotsee = chart.donotsee,
firstname = patient.firstname,
groupid = chart.groupid,
lastname = patient.lastname,
mailbox = patient.mailbox,
mi = patient.mi,
patient_recid = patient.recid,
phone = patient.phone,
selfpay = chart.selfpay,
cashonly = chart.cashonly,
sex = patient.sex,
ssn = patient.ssn,
state = patient.state,
street = patient.street,
zipcode = patient.zipcode
});
var r = (from c in q.AsEnumerable()
select new view_patient
{
birthdate = c.birthdate.ToShortDateString(),
chart_number = c.chart_number,
chart_recid = c.chart_recid,
city = c.city,
cpatient = c.cpatient,
donotsee = c.donotsee,
firstname = c.firstname,
groupid = c.groupid,
lastname = c.lastname,
mailbox = c.mailbox,
mi = c.mi,
patient_recid = c.patient_recid,
phone = c.phone,
selfpay = c.selfpay,
cashonly = c.cashonly,
sex = c.sex,
ssn = c.ssn,
state = c.state,
street = c.street,
zipcode = c.zipcode
}).SingleOrDefault();
return r;
}
}