ASP.NET MVC 4 joining 2 tables in stored Procedure but giving only one table's data - sql

I am new in ASP.NET MVC 4; Using DATABASE FIRST APPROACH. According to my question I am using SQL stored procedure to retrieve data to populate my jQgrid. But it is retrieving all BO tables data except OrgName which is coming from Org table(look in stored procedure). So What I do to get OrgName???
My Stored Procedure is as follows :
ALTER PROCEDURE [dbo].[GetBODetails]
AS
BEGIN
SELECT b.Id, b.OrgId, b.Code, b.Name, o.Name AS OrgName
FROM Org AS o INNER JOIN BO AS b ON o.Id = b.OrgId
END
Columns of BO table :
Id = GUID
Code = NVARCHAR(50)
Name = NVARCHAR(50)
OrgId = Foreign Key ref from Org table Id
Columns of BO table :
Id = GUID
Name = NVARCHAR(50)
Used this Procedure in my MVC project by doing "Add Function Import" from Model Browser window ;by referring this article Click here. While doing this I select Returns a collection of Entities is BO model.
Model generated of BO and Org are as follows :
BO.cs
public partial class BO
{
public System.Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public System.Guid OrgId { get; set; }
public virtual Org Org { get; set; }
}
Org.cs
public partial class Org
{
public Org()
{
this.BOes = new HashSet<BO>();
}
public System.Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<BO> BOes { get; set; }
}
Controller's code to execute Stored Procedure :
private iregEntities m_oDbCont = new iregEntities();
// GET: /BO/
public JsonResult BOGrid()
{
m_oDbCont.Configuration.ProxyCreationEnabled = false;
var BOList = m_oDbCont.GetBODetails().ToList(); // Executing stored procedure
var JsonBOList = new
{
rows = (
from BOData in BOList
select new
{
id = BOData.Id.ToString(),
cell = new string[] { BOData.OrgId.ToString(), BOData.Code, BOData.Name }
}).ToArray()
};
return Json(JsonBOList, JsonRequestBehavior.AllowGet); returning data into JSON format
}
protected override void Dispose(bool disposing)
{
m_oDbCont.Dispose();
base.Dispose(disposing);
}

While doing the function import, ask to create a new complex type instead of mapping the result to the BO entity. If you map to the BO entity, only this entity will be filled up with data, that's why Org is empty.
When mapping to a complex type, EF will automatically create a new class containing all the columns returned by the SP.
Mapping SP result to a specific entity is useful when the SP is returning that particular entity, it avoids creating useless complex types everytime you import an SP.

Related

ASP.NET MVC Cannot Implicitly Convert Type System.Collections.Generic.List<string> to 'string'

I'm trying to use EF to retrieve data from SQL and put it into a list of type viewmodel.
But when I try and retrieve the data it gives the following error: Cannot Implicitly Convert Type System.Collections.Generic.List<.string> to 'string'
I'm only trying to retrieve the ID and ProductName columns from the database.
Any help would be appreciated.
Here is my ViewModel:
public class ProductViewModel
{
public int ID { get; set; }
public string Product { get; set; }
}
Here is my controller code:
public ActionResult TableReport()
{
List<ProductViewModel> products = new List<ProductViewModel>();
ProductViewModel product = new ProductViewModel();
product.Product = db.Products.Select(z => z.ProductName).ToList();
product.ID = db.Products.Select(z => z.ProductID).ToList();
return View();
}
#GSerg: Read your code out loud. "Create an empty list of ProductViewModels. Create an empty ProductViewModel. Assign the list of all product names as the product name for that single empty ProductViewModel. Assign the list of all product ids as the id for that single empty ProductViewModel". No wonder it is not working, is it? Instead you should have done var products = db.Products.Select(z => new ProductViewModel() { ID = z.ProductID, Product = z.ProductName}).ToList();
You are putting a list in a string
Change your Viewmodel to
public class ProductViewModel
{
public int ID { get; set; }
public List<string> Product { get; set; }
}
And you are also trying to put a list in an int. That won't work either.

Multiple row insertion in asp.net core with dapper package

I want to perform multiple row insertion in asp.net core where dapper package is used. Here I use a model class, controller and another class. The result stored in database is a datatable which is converted from a list. Here I use a stored procedure with type.
public class PlanDetailsInputModel
{
public int MembershipPlanDetailsId { get; set; }
public int MembershipPlanId { get; set; }
public int CreatedBy { get; set; }
public string Flag { get; set; }
public List<dtPlanFeatureListModel> dtPricePlanDetails { get; set; }
}
public class dtPlanFeatureListModel
{
public long PricePlanFeatureId { get; set; }
}
This is my model class. Help me create my controller and the function.
If you have an insert statement and a List of objects that match up with the fields of the insert statement you can do something like the following.
public void UpdateDB()
{
List<Entity> entities = null;
entities = GetData();
var sql = #"INSERT INTO TABLE_01 ( Id, FieldA, FieldB ) VALUES ( #id, #FieldA, #Fieldb )";
using (var conn = (IDbConnection) GetDatabaseConnection())
{
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
using (var tx = conn.BeginTransaction())
{
//conn.Execute
conn.Execute(sql, entities, tx);
tx.Commit();
}
}
}
The code above will loop over all the rows in entities and perform the insert statements. If you leave out the transaction, then it will commit after each insert. But you include the transaction, it will commit after all the rows are inserted.
You can find more about it here
Although, your question doesn't say which class will be inserted. I don't think Dapper can handle your class PlanDetailsInputModel with the List inside it. You will have to flatten it to a DTO Class and use that DTO class in the Dapper command instead or use two dapper insert commands. One for PlanDetailsInputModel and another one for the List of dtPlanFeatureListModel.
HTH

One-to-Many relationship with ORMLite

The only examples I can find addressing this sort of scenario are pretty old, and I'm wondering what the best way is to do this with the latest version of ORMLite...
Say I have two tables (simplified):
public class Patient
{
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
}
public class Insurance
{
[Alias("InsuranceId")]
[Autoincrement]
public int Id { get; set; }
[ForeignKey(typeof("Patient"))]
public int PatientId { get; set; }
public string Policy { get; set; }
public string Level { get; set; }
}
Patients can have multiple Insurance policies at different "levels" (primary, secondary, etc). I understand the concept of blobbing the insurance information as a Dictionary type object and adding it directly to the [Patient] POCO like this:
public class Patient
{
public Patient() {
this.Insurances = new Dictionary<string, Insurance>(); // "string" would be the Level, could be set as an Enum...
}
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
public Dictionary<string, Insurance> Insurances { get; set; }
}
public class Insurance
{
public string Policy { get; set; }
}
...but I need the insurance information to exist in the database as a separate table for use in reporting later.
I know I can join those tables in ORMLite, or create a joined View/Stored Proc in SQL to return the data, but it will obviously return multiple rows for the same Patient.
SELECT Pat.Name, Ins.Policy, Ins.Level
FROM Patient AS Pat JOIN
Insurance AS Ins ON Pat.PatientId = Ins.PatientId
(Result)
"Johnny","ABC123","Primary"
"Johnny","987CBA","Secondary"
How can I map that into a single JSON response object?
I'd like to be able to map a GET request to "/patients/1234" to return a JSON object like:
[{
"PatientId":"1234",
"Name":"Johnny",
"Insurances":[
{"Policy":"ABC123","Level":"Primary"},
{"Policy":"987CBA","Level":"Secondary"}
]
}]
I don't have a lot of hope in this being do-able in a single query. Can it be done in two (one on the Patient table, and a second on the Insurance table)? How would the results of each query be added to the same response object in this nested fashion?
Thanks a ton for any help on this!
Update - 4/29/14
Here's where I'm at...In the "Patient" POCO, I have added the following:
public class Patient
{
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
[Ignore]
public List<Insurance> Insurances { get; set; } // ADDED
}
Then, when I want to return a patient with multiple Insurances, I do two queries:
var patientResult = dbConn.Select<Patient>("PatientId = " + request.PatientId);
List<Insurance> insurances = new List<Insurance>();
var insuranceResults = dbConn.Select<Insurance>("PatientId = " + patientResult[0].PatientId);
foreach (patientInsurance pi in insuranceResults)
{
insurances.Add(pi);
}
patientResult[0].Insurances = insurances;
patientResult[0].Message = "Success";
return patientResult;
This works! I get nice JSON with nested items for Insurances while maintaining separate related tables in the db.
What I don't like is that this object cannot be passed back and forth to the database. That is, I can't use the same nested object to automatically insert/update both the Patient and InsurancePolicy tables at the same time. If I remove the "[Ignore]" decorator, I get a field in the Patient table called "Insurances" of type varchar(max). No good, right?
I guess I'm going to need to write some additional code for my PUT/POST methods to extract the "Insurances" node from the JSON, iterate over it, and use each Insurance object to update the database? I'm just hoping I'm not re-inventing the wheel here or doing a ton more work than is necessary.
Comments would still be appreciated! Is Mythz on? :-) Thanks...
An alternate more succinct example:
public void Put(CreatePatient request)
{
var patient = new Patient
{
Name = request.Name,
Insurances = request.Insurances.Map(x =>
new Insurance { Policy = i.Policy, Level = i.Level })
};
db.Save<Patient>(patient, references:true);
}
References are here to save the day!
public class Patient
{
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
[Reference]
public List<Insurance> Insurances { get; set; }
}
public class Insurance
{
[Alias("InsuranceId")]
[Autoincrement]
public int Id { get; set; }
[ForeignKey(typeof("Patient"))]
public int PatientId { get; set; }
public string Policy { get; set; }
public string Level { get; set; }
}
I can then take a JSON request with a nested "Insurance" array like this:
{
"Name":"Johnny",
"Insurances":[
{"Policy":"ABC123","Level":"Primary"},
{"Policy":"987CBA","Level":"Secondary"}
]
}
...to create a new record and save it like this:
public bool Put(CreatePatient request)
{
List<Insurance> insurances = new List<Insurance>();
foreach (Insurance i in request.Insurances)
{
insurances.Add(new Insurance
{
Policy = i.Policy,
Level = i.Level
});
}
var patient = new Patient
{
Name = request.Name,
Insurances = insurances
};
db.Save<Patient>(patient, references:true);
return true;
}
Bingo! I get the new Patient record, plus 2 new records in the Insurance table with correct foreign key references back to the PatientId that was just created. This is amazing!
First you should define a foreign collection in Patient class. (with get and set methods)
#ForeignCollectionField
private Collection<Insurance> insurances;
When you query for a patient, you can get its insurances by calling getInsurances method.
To convert all into a single json object with arrays inside you can use a json processor. I use Jackson (https://github.com/FasterXML/jackson) and it works very well. Below will give you json object as a string.
new ObjectMapper().writeValueAsString(patientObject);
To correctly map foreign fields you should define jackson references. In your patient class add a managed reference.
#ForeignCollectionField
#JsonManagedReference("InsurancePatient")
private Collection<Insurance> insurances;
In your insurance class add a back reference.
#JsonBackReference("InsurancePatient")
private Patient patient;
Update:
You can use Jackson to generate objects from json string then iterate and update/create database rows.
objectMapper.readValue(jsonString, Patient.class);

PetaPoco returning incorrect ID

I have the following model and methods:
[PetaPoco.TableName("TestStep")]
[PetaPoco.PrimaryKey("ID")]
public class TestStep
{
public int ID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public string Details { get; set; }
}
public IEnumerable<TestStep> GetById(int ID)
{
var db = new PetaPoco.Database("TestProcedureDB");
return db.Query<TestStep>(#"SELECT * FROM TESTSTEP TS
INNER JOIN TESTSTEPLINK L ON L.STEPID = TS.ID
WHERE L.TESTID = #0", ID);
}
When the POCO is populated, the ID property value is that of the ID column in the TESTSTEPLINK table. If I change the query to return SELECT TS.* then all is ok. Is this a bug or am I missing something?
PetaPoco will go through all your return columns and map them.
First it will map Id from the table TESTSTEP, then it finds Id again and so it overrides the previously set value.
If you are doing a join like this and only want specific information, you should either only specify the columns you want to return (otherwise you are bringing back more data than needed which is a performance issue)
or do as you did to fix it by using TS.* to ensure only the columns from the first table are mapped.

NHibernate - query specific columns and return distinct records?

I am new to NH.
I have a table in a legacy DB that looks like this:
Id,
CompanyId,
Description,
[LOADS of other columns here]
I would like to return a DISTINCT set of data using NHibernate, selecting only specific columns and using a WHERE statement. The SQL would looks something like this:
SELECT DISTINCT
[table_name].CompanyId,
[table_name].Description
FROM
[table_name]
WHERE
[table_name].CompanyId = 2
Having googled this I came up with:
ProjectionList projections = Projections.ProjectionList();
projections.Add(Projections.Property("CompanyId"), "CompanyId");
projections.Add(Projections.Property("Name"), "SomeName");
var companyDto = session.QueryOver<Company>()
.Where(x => x.CompanyId == 2)
.Select(projections)
.TransformUsing(Transformers.AliasToBean<CompanyDto>())
.List<CompanyDto>();
if (companyDto != null)
Console.WriteLine(string.Format("{0}, {1}", companyDto.CompanyId, companyDto.SomeName));
Where the DTO is:
public class CompanyDto
{
public int CompanyId { get; set; }
public string SomeName { get; set; }
}
And the entity is:
public class Company
{
public virtual int Id { get; private set; }
public virtual int CompanyId { get; set; }
public virtual string Name { get; set; }
}
This does not bring back disinct records. I know that normally I would have to use a different transform (DistinctRootEntity) but I cannot use two transforms. How can I combine all of the things I want, into a single call? It must be possible, its basic SQL ....
I need to:
not use HQL
not bring back all columns for the record
not bring back duplicate rows
there is a Projection for this
var projections = Projections.Distinct(Projections.ProjectionList()
.Add(Projections.Property("CompanyId").As("CompanyId"))
.Add(Projections.Property("Name").As("SomeName"));