We have two models vw_fleet and fleet_contact generated by the entity framework. We are using views to retrieve data and we can't define the relationship between those models but when we retrieve data for fleet_contact we need fleet information as well.
public partial class vw_fleet
{
public int account_id { get; set; }
public int fleet_id { get; set; }
public string fleet_name { get; set; }
}
public partial class fleet_contact
{
public int id { get; set; }
public int fleet_id { get; set; }
public string contact { get; set; }
}
We have added a property(Fleet) to fleet_contact
public partial class fleet_contact
{
public vw_fleet Fleet { get; set; }
}
One way to do that is by using join.
using (var context = new EFEntities())
{
return context.fleet_contact.Join(context.vw_fleet, fc => fc.fleet_id, f => f.fleet_id, (fc, f) => new FleetContactModel()
{
fleet_id = fc.fleet_id,
Fleet = f,
contact = fc.contact,
id = fc.id,
}
).ToList();
}
Is there any way to achieve the above solution.
Related
How can i fix Cannot implicitly convert System.Collections.Generic.List <RPHistory> to System.Collections.Generic.List <RPHistory> exception error.
I am trying to combine two Entities together to get a single list
RP Entity Class:
public class RP
{
public int ID { get; set; }
public int RPID { get; set; }
public string Name { get; set; }
public int ProductID { get; set; }
}
RPHistory Entity Class :
public class RPHistory:
{
public int ID { get; set; }
public int RPID { get; set; }
public string Name { get; set; }
public int ProductID { get; set; }
}
And I created this third class
RpWithHistory Class :
public class RpWithHistory {
public int ID;
public int RPID;
public string Name;
public int ProductID;
public List<RPHistory> History;
}
Linq Query
var RPs = await Context.RP.Where(b => b.ProductID == request.ID)
.Select(x=> new RpWithHistory {
ID = x.ID,
RPID = x.RPID,
Name = x.Name,
ProductID = x.ProductID,
History = Context.RPHistory
.Where(y=> y.RPID
== x.RPID)
.ToList()
}
).ToListAsync();
But i get this error,
>Cannot implicitly convert System.Collections.Generic.List <RPHistory> to
>System.Collections.Generic.List <RPHistory> exception error
Thanks!
I am not sure why you're doing that. Can I suggest this?
You do not need to go all that way of creating a class that joins the two. Just create a Navigation property on your RP that points to RPHistory Objects.
public class RP
{
public int ID { get; set; }
public int RPID { get; set; }
public string Name { get; set; }
public int ProductID { get; set; }
public ICollection<RPHistory> HistoryList { get; set; } // Navigation Property
}
public class RPHistory:
{
public int ID { get; set; }
public int RPID { get; set; }
public string Name { get; set; }
public int ProductID { get; set; }
[ForeignKey(nameof(RPID))] // Identify the Foreign Key from RP Class
public RP RP { get; set; } // Navigation back to RP
}
Then you can chain everything into a single list using LINQ:
var RPs = Context.RP.Where(rp => rp.ProductID == request.ID)
.Include(rp=>rp.RPHistory) // This includes RPHistory
.ToList();
You need to clone or create a new list.
Option 1: Use ConvertAll
List<RPHistory> pPHistoryCopy = rphWithHistory.RPHistory.ConvertAll(history => new RPHistory(rphWithHistory.RPHistory));
Option 2:
//Clone Extension
static class Extensions
{
public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
{
return listToClone.Select(item => (T)item.Clone()).ToList();
}
}
Use the clone extention
I am using Entity Framework to deal with database, and I have self-referencing model as following:
public class PhysicalObject
{
public PhysicalObject()
{
SubPhysicalObjects = new HashSet<PhysicalObject>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int? ParentId { get; set; }
[StringLength(150)]
public string Title { get; set; }
public virtual PhysicalObject Parent { get; set; }
public virtual ICollection<PhysicalObject> SubPhysicalObjects { get; set; }
}
I was using GraphDiff library to update disconnected graphs but it seems it does not support updating self-referencing graphs.
My question is: what is the best way to update self-referencing graphs using Entity Framework by:
Deleting/Updating existing physicalObjects
Inserting not existing physicalObjects
Let's say I have two entities as followings:
public class PhysicalObject
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int? ParentId { get; set; }
public int StorageRequestId { get; set; }
public string Title { get; set; }
public virtual PhysicalObject Parent { get; set; }
public virtual ICollection<PhysicalObject> SubPhysicalObjects { get; set; }
public virtual StorageRequest StorageRequest { get; set; }
}
public class StorageRequest
{
public StorageRequest()
{
PhysicalObjects = new HashSet<PhysicalObject>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<PhysicalObject> PhysicalObjects { get; set; }
}
Notice that PhysicalObject is self-referencing table .
Not let's update graphs using entity framework:
var oldPhysicalObjects = dbContext.PhysicalObjects.Where(x => x.StorageRequestId== storageRequestId).ToList();
var existingIds = new HashSet<int>();
foreach (var item in newGraphDto.PhysicalObjects.ToList())
{
updateGraph(item, oldPhysicalObjects, dbContext, storageRequestId,existingIds);
}
var posToDelete = oldPhysicalObjects.Where(x => existingIds.All(e => e != x.Id)).ToList();
dbContext.PhysicalObjects.RemoveRange(posToDelete);
dbContext.SaveChanges();
updateGraph method will update every tree of PhysicalObjects recursively and it looks like:
private void updateGraph(PhysicalObjectDto physicalObjectDto, IList<PhysicalObject> oldPhysicalObjects, MyDbContext dbContext, int storageRequestId, HashSet<int> existingIds, PhysicalObject parent = null)
{
if (physicalObjectAddEditDto.Id == 0)
{
PhysicalObject po = new PhysicalObject
{
Id = physicalObjectAddEditDto.Id,
Title = physicalObjectAddEditDto.Title,
StorageRequestId = storageRequestId,
Parent=parent
};
dbContext.PhysicalObjects.Add(po);
parent = po;
}
else
{
var po = oldPhysicalObjects.FirstOrDefault(x => x.Id == physicalObjectAddEditDto.Id);
po.Title = physicalObjectAddEditDto.Title;
po.StorageRequestId = storageRequestId;
po.Parent = parent;
dbContext.Entry(po).CurrentValues.SetValues(po);
parent = po;
}
existingIds.Add(parent.Id);
foreach (var subPhysicalObject in physicalObjectAddEditDto.SubPhysicalObjects)
{
updateGraph(subPhysicalObject, oldPhysicalObjects, dbContext, mailRoomRequestId, existingIds, parent);
}
}
I hope my code will help others to know how to update graph trees of self-referencing table
Title says it all, right now I have a single table that is populated from a linq query and I want to add more tables with different linq queries. I'm kinda lost on how would I do that..
I could probably do it if I create different views for each table but I want to have just one view for all. :D
Here's my code: (It's a table for "on going" projects)
Controller:
public ActionResult Index()
{
var project = from x in db.Projects
where x.Project_Status == "Ongoing"
select x;
return View(project);
}
Model:
public class Project
{
[Key]
public int Project_Id { get; set; }
public string Project_Name { get; set; }
public string Project_Detail { get; set; }
public string Project_Status { get; set; }
public int Employee_Id { get; set; }
}
View Model:
public class AdminHomeViewModel
{
public Project Ongoing { get; set; } //table for ongoing projects
public Project NYA { get; set; } //another table for Not Yet Assigned projects
public Employee Free { get; set; } //another table for free employees
public List<Project> OngoingList { get; set; }
public List<Employee> NYAList { get; set; }
public List<Employee> FreeList { get; set; }
}
You are confusing yourself with the different types of models. You should have a clear understanding between View-Model and Data-Model. You should always return View-Model to the View, and not the Data-Model. Data-Model are just the POCO classes which represents your data framework (in this case, each tables). There should be different Data Models for each of your table, which you must be having already based on your entity-framework approach (Code first, Model first or Database first). Then, prepare a single model for your view (as we can bind only one model to one view). Keep all the fields from different Data-Models that you need in that View and pass it along. See the approach below:
Data-Models
public class Project
{
[Key]
public int Project_Id { get; set; }
public string Project_Name { get; set; }
public string Project_Detail { get; set; }
public string Project_Status { get; set; }
public int Employee_Id { get; set; }
}
public class Employee
{
[Key]
public int Employee_Id { get; set; }
public string Employee_Name { get; set; }
public string Employee_Detail { get; set; }
}
View-Model
public class MyViewModel
{
public int Project_Id { get; set; }
public string Project_Name { get; set; }
public string Project_Detail { get; set; }
public string Project_Status { get; set; }
public int Employee_Id { get; set; }
public string Employee_Name { get; set; }
public string Employee_Detail { get; set; }
}
Or
public class MyViewModel
{
public Project proj { get; set; }
public Employee emp { get; set; }
}
Pass it to view as:
public ActionResult Index()
{
MyViewModel model = new MyViewModel();
// You linq query to populate model goes here
return View(model);
}
Update:
From my understanding, you need something like this:
View-Model:
public class AdminHomeViewModel
{
public AdminHomeViewModel()
{
Ongoing = new List<Project>();
NYA = new List<Project>();
Free = new List<Employee>();
}
public List<Project> Ongoing { get; set; } //table for ongoing projects
public List<Project> NYA { get; set; } //another table for Not Yet Assigned projects
public List<Employee> Free { get; set; } //another table for free employees
}
Controller:
public ActionResult Index()
{
AdminHomeViewModel model = new AdminHomeViewModel();
var result1 = (from x in db.Projects
where x.Project_Status == "Ongoing"
select new Project(){
Project_Id = x.Project_Id ,
Project_Name = x.Project_Name,
... //all other assignments goes here
}).ToList();
var result2 = (from x in db.Projects
where x.Project_Status == "blah blah"
select new Project(){
Project_Id = x.Project_Id ,
Project_Name = x.Project_Name,
... //all other assignments goes here
}).ToList();
var result3 = (from x in db.Employee
where x.AnyCondition == "blah blah"
select new Employee(){
Employee_Id = x.Employee_Id ,
Employee_Name = x.Employee_Name,
... //all other assignments goes here
}).ToList();
model.Ongoing = result1;
model.NYA = result2;
model.Free = result3;
return View(model);
}
Hi im making a project using asp.net mvc 4.0 , with entity framework and linq...
So i have two classes.
Cliente Class:
namespace LicenciamentoMVC.Models
{
public class Cliente
{
[Key]
public int IDCliente { get; set; }
public string Nome { get; set; }
public string Morada { get; set; }
public string CPostal { get; set; }
public string Localidade { get; set; }
public string Freguesia { get; set; }
public string Conselho { get; set; }
public string Distrito { get; set; }
public string Pais { get; set; }
public string Telefone { get; set; }
public string Telemovel { get; set; }
public string Fax { get; set; }
public string Email { get; set; }
public string Nif { get; set; }
public string WWW { get; set; }
public string Observacoes { get; set; }
public int IDP { get; set; }
public int IDU { get; set; }
public DateTime TStamp { get; set; }
public int Rem { get; set; }
public String TipoCliente { get; set; }
}
public class ClienteModel
{
private static Cliente entity;
public static IQueryable<Cliente> GetListaClientes()
{
MvcApplication1Context db = new MvcApplication1Context();
var customers = from c in db.Clientes
orderby c.IDCliente descending
where c.Rem==0
select c;
return customers.AsQueryable<Cliente>();
}
}
}
and here is my Processo Class
public class Processo
{
[Key]
public int IDProcesso { get; set; }
public int IDCliente { get; set; }
public DateTime DataInserido { get; set; }
public string NumeroFactura { get; set; }
public DateTime DataFactura { get; set; }
public string Estado { get; set; }
public int IDU { get; set; }
public int Rem { get; set; }
public DateTime TStamp { get; set; }
}
Lets say i want to view the the IDprocesso and DataInserido from class Processo, and Nome from class Cliente.
My database is composed of two tables Clientes and Processos.
My dbContent Class:
public class MvcApplication1Context:DbContext
{
public MvcApplication1Context()
: base("name=MvcApplication1Context")
{
}
//protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
// modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//}
public DbSet<Cliente> Clientes { get; set; }
public DbSet<Processo> Processos { get; set; }
}
The method GetListClientes is an iqueryable because i am using Igniteui components, in this case igGrid and i followed a tutorial that i found in the infragistics site, if there is a better way to do this, please tell me..
Back to the topic..
I didnt declare the foreign keys because of problem with contrains..
Should i create a new class with the fields that i want to see , or is there another way.
if i go with the method of create a new class for showing the data i should do something like this inside that class:
public class ProcessoClienteModel
{
private static ProcessoCliente entity;
public static IQueryable<ProcessoCliente> GetListaProcessos()
{
MvcApplication1Context db = new MvcApplication1Context();
var processos = from p in db.Processos
from c in db.Clientes
orderby p.IDProcesso descending
where p.IDCliente == c.IDCliente
where p.Rem == 0
select new { p.processoID,p.DataInserido,c.Nome} as IQueryable<ClienteModel>;
return processos;
return processos.AsQueryable<ProcessoCliente>();
}
}
Or is there a better way for doing this...
I am going through topics on internet trying to find a way..
Thanks for the help..
So i have been trying to find a way and the thing that i am trying to make is a viewmodel class, in this view model i will have the fields that i want to display, the Processo id, Data Inserido and the name of the cliente..and this class wil fetch value through linq to my domain class, in this case processo and cliente...
Am i going in the right direction???
Using the tip that Bjorn Vdkerckhove gave me, thanks, i have done this..
Create a new folder called viewmodel, and inside create a new class called ProcessoCliente.cs
The code of this class:
namespace LicenciamentoMVC.ModelsView
{
public class ProcessoCliente
{
public int IDProcesso { get; set; }
public string NomeCliente { get; set; }
public DateTime DataInserido { get; set; }
}
public class ProcessoModel
{
private static ProcessoCliente entity;
public static IQueryable<ProcessoCliente> GetListaProcessosClientes()
{
MvcApplication1Context db = new MvcApplication1Context();
var processos = (from p in db.Processos
join c in db.Clientes on p.IDCliente equals c.IDCliente
orderby p.IDProcesso descending
select new ProcessoCliente { IDProcesso = p.IDProcesso, NomeCliente = c.Nome, DataInserido = p.DataInserido});
return processos.AsQueryable<ProcessoCliente>();
}
}
}
then i create the view on the index action of controller Processo
#model IEnumerable<LicenciamentoMVC.ModelsView.ProcessoCliente>
#using Infragistics.Web.Mvc
#{
ViewBag.Title = "Index";
}
#* render of ignite ui grid *#
#( Html.Infragistics().Grid<LicenciamentoMVC.ModelsView.ProcessoCliente>()
.Caption("Processos")
.ID("grid1")
.DefaultColumnWidth("200px")
.PrimaryKey("IDProcesso")
.Columns(column =>
{
column.For(x =>x.NomeCliente).DataType("string").HeaderText("Nome do Cliente").Width("60%");
column.For(x => x.DataInserido).DataType("DateTine").HeaderText("Data de Criação do Processo").Width("40%");
column.For(x => x.IDProcesso).DataType("int").Width("0%");
})
.Features(features =>
{
features.Paging().PageSize(20).PrevPageLabelText("Previous").NextPageLabelText("NEXT");
features.Sorting().Mode(SortingMode.Single).ColumnSettings(settings =>
{
settings.ColumnSetting().ColumnKey("NomeCliente").AllowSorting(true);
});
features.Selection().MultipleSelection(false).Mode(SelectionMode.Row);
features.Filtering().Mode(FilterMode.Simple);
features.Updating()
.EnableAddRow(false)
.EnableDeleteRow(true)
.EditMode(GridEditMode.None);
})
.DataSourceUrl(Url.Action("ListarProcessos"))
.UpdateUrl(Url.Action("DeleteProcessos"))
.AutofitLastColumn(false)
.Width("100%")
.AutoGenerateColumns(false)
.DataBind()
.Render()
)
And i have this action inside ProcessoController to load the grid..
[GridDataSourceAction]
public ActionResult ListarProcessos()
{
return View(LicenciamentoMVC.ModelsView.ProcessoModel.GetListaProcessosClientes());
}
It works, but i would like to know if this is the right way to go...or there is a better way to do it..
Thanks again,..
You don't have any real relation between the objects if i understand you correctly. It all depends on how much data you need to show.
If you want to show related data, you can do this on your view with razor:
#foreach(var client in Model.Clientes)
{
<h1>#client.Name</h1>
<ul>
<li>Model.Processo.FirstOrDefault(f=> f.IDCliente == client.IDCliente).DataInserido.toString("dd-MM-yyyy")</li>
<li>.... other properties...</li>
</ul>
}
I have made simple model for example.
public class Publisher
{
public int Id { get; set; }
public string Title { get; set; }
public Address Location { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Address
{
public string Country { get; set; }
public string City { get; set; }
public string Street { get; set; }
public string HouseNumber { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public int LanguageId { get; set; }
public int? PublisherId { get; set; }
}
I need to get publishers with related books. I know how to do it using linq to entities. Is it possible to solve a problem using entity sql?
public class CatalogContext : DbContext {...}
public List<Publisher> GetByCity(string city)
{
var result = new List<Publisher>();
string queryString;
queryString = String.Format(#"SELECT VALUE row(a,b)
FROM CatalogContext.Publishers AS a
join CatalogContext.Books AS b on a.Id = b.PublisherId
WHERE a.Location.City = '{0}'", city);
var rows = ((IObjectContextAdapter)_context).ObjectContext.CreateQuery<DbDataRecord>(queryString).ToList();
return ???
}
Query returns required data but it's List<DbDataRecord> - list of pairs <publisher, book>. How to translate it to list of publishers with filled navigation property "Books"?
Is it possible to write query which directly returns List<Publisher>?
you can do the following:
var result = ObjectContext.Publishers.Include("Books").Include("Locations")
.Where(c => c.Location.City = "SOME_CITY").Select(c => c);
Include - basically joins the table.
Then you can drill down to books by doing the following:
var test = result[0].Books;
Why are you using direct sql command instead of Entity Framework code style?