Entity Framework one to many: cannot implicitly convert - c#

I have this two POCO clases the Inventory and e Lot for details
How could I retrieve the data as Inventory to => Many lots
public class Inventory
{
// This determine the One to Many RelationShip
public Inventory()
{
this.Lots = new HashSet<Lot>();
}
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required]
public string ItemID { get; set; }
public string Description { get; set; }
public Nullable<DateTime> CreateDate { get; set; }
public string CreateUser { get; set; }
public decimal LastCost { get; set; }
public bool MonitorLevel { get; set; }
public short MinLevel { get; set; }
public short MaxLevel { get; set; }
public string GTIN { get; set; }
public decimal Weight { get; set; }
public string UOM { get; set; }
// Navigation Property
public virtual ICollection<Lot> Lots { get; set; }
}
public class Lot
{
public int Id { get; set; }
public Nullable<DateTime> CreateDate { get; set; }
public string CreateUser { get; set; }
public Nullable<DateTime> ExpDate { get; set; }
public string LotSerial { get; set; }
public virtual Inventory Inventory { get; set; }
}
I try this but there is an error con the conversion type
public class InventoryController : ApiController
{
private FarmStoreContext db = new FarmStoreContext();
// Project Inventory to inventory DTOs.
private IQueryable<InventoryDTO> MapInventories()
{
return from i in db.Inventories
select new InventoryDTO() { Id = i.Id, Description = i.Description, ItemID = i.ItemID, GTIN = i.GTIN, LastCost = i.LastCost, Weight = i.Weight, UOM = i.UOM};
}
public IEnumerable<InventoryDTO> GetInventories()
{
return MapInventories().AsEnumerable();
}
public InventoryDTO GetInventory(int Id)
{
Inventory inventory = db.Inventories;// <== Error - Here can not implicity convert type System.Data.Entiry.Dbset<....Models.Inventory> To ....Models.Inventory
//var inventory = (from i in MapInventories()
// where i.Id == Id
// select i).FirstOrDefault();
if (inventory == null)
{
throw new HttpResponseException(
Request.CreateResponse(HttpStatusCode.NotFound));
}
return new InventoryDTO()
{
DetaislLots = from d in inventory.Lots
select new InventoryDTO.DetaislLot()
{
LotSerial = d.LotSerial,
LIFOdate = d.LIFOdate,
QtyOriginal = d.QtyOriginal,
QtyAllocated = d.QtyAllocated,
QtyOnHand = d.QtyOnHand,
QtyAvailable = d.QtyAvailable,
Status = d.Status,
LineComment = d.LineComment,
UnitCost = d.UnitCost,
ReceiptDate = d.ReceiptDate
}
};
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}

db.Inventories is a DbSet<Inventory>, which is a collection, while you are expecting a single Inventory. You have to execute a query which returns a single Inventory. Try:
Inventory inventory = db.Inventories.FirstOrDefault(i => i.Id == Id);
You basically have this query in a comment below that line. You can use the navigation property Lots on an Inventory to get the related Lot objects:
List<Lot> lots = inventory.Lots.ToList();

Related

Linq query to object cannot pass Xunit test

I have strange problem. I have to pass the following Xunit test:
Fact]
public void GetOrder_ValidId_ReturnsCompleteOrder()
{
var service = new DataService();
var order = service.GetOrder(10248);
Assert.Equal(3, order.OrderDetails.Count);
Assert.Equal("Queso Cabrales", order.OrderDetails.First().Product.Name);
Assert.Equal("Dairy Products", order.OrderDetails.First().Product.Category.Name);
}
I am able to pass the orderdetails list to the test. However the "orderdetail" objects that i pass have a product object with null.
My classes are:
public class Order
{
[Key] public int Id { get; set; }
public DateTime Date { get; set; }
public DateTime Required { get; set; }
[Required] public virtual ICollection<OrderDetail> OrderDetails { get; set; }
public string ShipName { get; set; }
public string ShipCity { get; set; }
public Order()
{
this.OrderDetails = new List<OrderDetail>();
}
public override string ToString()
{
string ret =
$"Id = {Id}, DateTime = {Date}, Required = {Required}, shipName= {ShipName}, Shipcity = {ShipCity}";
return ret;
}
}
public class OrderDetail
{
public Order Order { get; set; }
[ForeignKey("orders")]
public int Orderid { get; set; }
[ForeignKey("products")]
public int productid { get; set; }
public int UnitPrice { get; set; }
public int Quantity { get; set; }
public int Discount { get; set; }
[Required] public virtual Product Product { get; set; }
public OrderDetail()
{
Product = Product;
}
public override string ToString()
{
return
$"OrderId = {Orderid}, Productid = {productid}";
}
}
public class Product
{
[ForeignKey("orderdetails")] public int Id { get; set; }
public string Name { get; set; }
public float UnitPrice { get; set; }
public string QuantityPerUnit { get; set; }
public int UnitsInStock { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
public override string ToString()
{
return
$"Id = {Id}, Name = {Name}, UnitPrice = {UnitPrice}, QuantityPerUnit = {QuantityPerUnit}, UnitsInStock = {UnitsInStock}, CategoryId = {CategoryId}";
}
}
I have the tried the following two solution:
public Order GetOrder(int id)
{
using var ctx = new NorthWindContext();
var query = ctx.Orders.AsQueryable().Where(o => o.Id == 10248).FirstOrDefault();
ctx.SaveChanges();
//var query2 = ctx.Orders.Include("orderdetails").Where()
return query;
}
and
var query2 = ctx.Orders.Where(o => o.Id == 10248)
.Select(a => new Order
{
Id = a.Id,
OrderDetails = a.OrderDetails
}).FirstOrDefault();
I have tried to reconfigure the mapping but didn't do it.
If i do the same query and use Console.Writeline in a foreach loop i can conclude that every orderdetail has a "product"...
I keep on the getting the error: "Object not set to an instance of an object" when passing to xUnit test.
Ivans response did the job.
First of all i removed using System.Data.Entity;
Then i changed the order class to the following:
public class Order
{
[Key] public int Id { get; set; }
public DateTime Date { get; set; }
public DateTime Required { get; set; }
[Required] public virtual List<OrderDetail> OrderDetails { get; set; }
public string ShipName { get; set; }
public string ShipCity { get; set; }
public Order()
{
this.OrderDetails = new List<OrderDetail>();
}
public override string ToString()
{
string ret =
$"Id = {Id}, DateTime = {Date}, Required = {Required}, shipName= {ShipName}, Shipcity = {ShipCity}";
return ret;
}
}
Then i used the following query just like Ivans:
var query3 = ctx.Orders
.Include(o => o.OrderDetails)
.ThenInclude(d => d.Product)
.ThenInclude(d => d.Category)
.AsSingleQuery()
.FirstOrDefault(o => o.Id == 10248);

Linq nesting relationships and DTOs

I'm getting data from an Oracle database and creating an API for internal use. Some of the classes are (shortened for brevity):
CustomerOrderTab
Public partial class CustomerOrderTab
Public string OrderNo { get; set; }
[ForeignKey("CustomerMV")]
public string CustomerNo { get; set; }
public List<CustomerOrderLineTab> CustomerOrderLines { get; set; }
public List<CustomerOrderChargeTab> CustomerOrderCharge { get; set; }
public List<ShipmentOrderLineTab> ShipmentOrderLines { get; set; }
public CustomerMV CustomerMV { get; set; }
CustomerOrderDTO
Public class OrderDTO
public string OrderNo { get; set; }
public string CustomerName { get; set; }
public List<OrderLinesDTO> OrderLines { get; set; }
public List<OrderChargesDTO> OrderCharges { get; set; }
public List<ShipmentOrderLinesDTO> ShipmentOrderLines { get; set; }
ShipmentOrderLineTab
public partial class ShipmentOrderLineTab
public decimal ShipmentId { get; set; }
[ForeignKey("CustomerOrderTab")]
public string OrderNo { get; set; }
public ShipmentTab Shipment { get; set; }
ShipmentOrderLineDTO
public class ShipmentOrderLinesDTO
public decimal ShipmentId { get; set; }
public string OrderNo { get; set; }
public ShipmentTab MyShipment { get; set; }
ShipmentTab
public partial class ShipmentTab
[ForeignKey("ShipmentOrderLineTab")]
public decimal ShipmentId { get; set; }
public string ShipperAddress1 { get; set; }
public string ShipperAddress2 { get; set; }
public List<ShipmentHandlingUnitTab> ShipmentHandlingUnits { get; set; }
ShipmentDTO
public class ShipmentDTO
public decimal ShipmentId { get; set; }
public string ShipViaCode { get; set; }
public string ShipmentPayer { get; set; }
public List<ShipmentHandlingUnitDTO> ShipmentHandlingUnitDTOs { get; set; }
This Works:
var orderLines = from o in _context.CustomerOrderTab
.Where(o => o.OrderNo == orderno)
select new OrderDTO()
{
OrderNo = o.OrderNo,
CustomerName = o.CustomerMV.CustomerName,
This is a child of the order ShipmentOrderLines = o.ShipmentOrderLines
This works .Select(ob => new ShipmentOrderLinesDTO
{
ShipmentId = ob.ShipmentId,
OrderNo = ob.OrderNo,
MyShipment = ob.Shipment
Adding this does not work .Select(y => new ShipmentDTO
The error says that ShipmentTab
Doesn’t have a definition for Select {
}
}).ToList()
Shipment is a child of ShipmentOrderLine (there will only be one shipment per line)
ShipmentOrderLine is a child of CustomerOrder (there can be many lines per order)
I think the problem is that there is only one shipment per line but I've tried lots of things and can't get it to map to my DTO.
Fix your model:
ShipmentOrderLineDTO
public class ShipmentOrderLinesDTO
public decimal ShipmentId { get; set; }
public string OrderNo { get; set; }
public ShipmentDTO MyShipment { get; set; }
Select is only used to project a collection. For non-collections, you just create a new object...
var orderLines = _context.CustomerOrderTab
.Where(o => o.OrderNo == orderno)
.Select(o => new OrderDTO {
OrderNo = o.OrderNo,
CustomerName = o.CustomerMV.CustomerName,
ShipmentOrderLines = o.ShipmentOrderLines
.Select(ob => new ShipmentOrderLinesDTO {
ShipmentId = ob.ShipmentId,
OrderNo = ob.OrderNo,
MyShipment = new ShipmentDTO {
ShipmentId = ob.Shipment.ShipmentId,
...
ShipmentHandlingUnits = ob.Shipment.ShipmentHandlingUnits
.Select(shu=> new ShipmentHandlingUnitDTO {
...
}).ToList()
}
}
}).ToList()
Now with that all said, I typically have mapping extension methods on my data model classes, which allow things like this:
var orderLines = _context.CustomerOrderTab
.Where(o=>o.OrderNo == orderno)
.Select(o=> o.ToDto());
Example class:
public static class CustomerOrderTabExtensions {
public static CustomerOrderDto ToDto(this CustomerOrderTab cot) {
return new CustomerOrderDto {
OrderNo = cot.OrderNo,
...
OrderLines = cot.CustomerOrderLines
.Select(col=>col.ToDto())
.ToList()
}
}
}
Or use Automapper.

Mapping Viewmodel to model using AutoMapper

I have written a controller method in asp.net api that would return a viewmodel called AllocationsViewModel. The GetAllocationsViewModel contains subsets of three more viewmodels. The GetAllocationsGrouped currently returns FIRMWIDE_MANAGER_ALLOCATION and I need to return this FirmWideAllocationsViewModel instead. I have installed Automapper 8.0 and added some code to do the mapping. Is that enough to do the job. I can see only the ManagerStrategyID and ManagerStrategyID values coming through the values are comming null for the fields. I have run the original query and can see there are values for all the fields
public class FIRMWIDE_MANAGER_ALLOCATION
{
private decimal _groupPercent;
public int FIRM_ID { get; set; }
public string FIRM_NAME { get; set; }
public int? MANAGER_STRATEGY_ID { get; set; }
public int? MANAGER_FUND_ID { get; set; }
public int MANAGER_ACCOUNTING_CLASS_ID { get; set; }
public int? MANAGER_FUND_OR_CLASS_ID { get; set; }
public string MANAGER_FUND_NAME { get; set; }
public string MANAGER_ACCOUNTING_CLASS_NAME { get; set; }
public string MANAGER_STRATEGY_NAME { get; set; }
public int? PRODUCT_ID { get; set; }
public string PRODUCT_NAME { get; set; }
public int? QUANTITY { get; set; }
public decimal? NAV { get; set; }
}
public class FirmWideAllocationsViewModel
{
private decimal _groupPercent;
public int FirmID { get; set; }
public string FirmName { get; set; }
public int? ManagerStrategyID { get; set; }
public int? ManagerFundID { get; set; }
public int ManagerAccountClassID{ get; set; }
public int? ManagerFundOrClassID { get; set; }
public string ManagerFundName { get; set; }
public string ManagerAccountingClassName { get; set; }
public string ManagerStrategyName { get; set; }
public int? ProductID { get; set; }
public string ProductName { get; set; }
public int? Quantity { get; set; }
public decimal? Nav { get; set; }
}
public IHttpActionResult Details(int id, DateTime date)
{
var viewModel = GetAllocationsViewModel(id, date);
if (viewModel == null) return NotFound();
return Ok(viewModel);
}
private AllocationsViewModel GetAllocationsViewModel(int id, DateTime date)
{
var ms = GetStrategy(id);
DateTime d = new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1);
if (ms.FIRM_ID != null)
{
var firm = GetService<FIRM>().Get(ms.FIRM_ID.Value);
var currentEntity = new EntityAllocationsViewModel(new EntityViewModel { EntityId = firm.ID, EntityName = firm.NAME, EntityType = EntityType.Firm });
var allocationsGrouped = Mapper.Map<List<FIRMWIDE_MANAGER_ALLOCATION>, List<FirmWideAllocationsViewModel>>(GetAllocationsGrouped(EntityType.ManagerStrategy, id, d).ToList());
var missingProducts = GetMissingProducts();
var vm = new AllocationsViewModel
{
CurrentEntity = currentEntity,
ManagerAllocations = allocationsGrouped,
MissingProducts = missingProducts
};
return vm;
}
return null;
}
public class AllocationsViewModel
{
public EntityAllocationsViewModel CurrentEntity { get; set; }
public List<FirmWideAllocationsViewModel> ManagerAllocations { get; set; }
public object MissingProducts { get; set; }
}
I have added the following code after installing autommapper 8.0
public class AutoMapperConfig
{
public static void Initialize()
{
Mapper.Initialize((config) =>
{
config.ReplaceMemberName("FIRM_ID", "FirmID");
config.ReplaceMemberName("FIRM_NAME", "FirmName");
config.ReplaceMemberName("MANAGER_STRATEGY_ID", "ManagerStrategyID");
config.ReplaceMemberName("MANAGER_FUND_ID", "ManagerFundID");
config.ReplaceMemberName("MANAGER_ACCOUNTING_CLASS_ID", "ManagerAccountClassID");
config.ReplaceMemberName("MANAGER_FUND_OR_CLASS_ID", "ManagerFundOrClassID");
config.ReplaceMemberName("MANAGER_FUND_NAME", "ManagerFundName");
config.ReplaceMemberName("MANAGER_ACCOUNTING_CLASS_NAME", "ManagerAccountingClassName");
config.ReplaceMemberName("MANAGER_STRATEGY_NAME", "ManagerStrategyName");
config.ReplaceMemberName("PRODUCT_ID", "ProductID");
config.ReplaceMemberName("PRODUCT_NAME", "ProductName");
config.ReplaceMemberName("QUANTITY", "Quantity");
config.ReplaceMemberName("NAV", "Nav");
config.CreateMap<FIRMWIDE_MANAGER_ALLOCATION, FirmWideAllocationsViewModel>().ReverseMap();
});
}
}
protected void Application_Start()
{
AutoMapperConfig.Initialize();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
The issue has been resolved. I had to amend the grouping statement that is called to include all the fields . It was working fine earlier but with the upgrade of the latest entity framework, I think its the case
allocations = allocations.GroupBy(x => new { x.MANAGER_STRATEGY_ID, x.PRODUCT_ID, x.EVAL_DATE })
.Select(group => new FIRMWIDE_MANAGER_ALLOCATION { EVAL_DATE = group.First().EVAL_DATE,
FIRM_ID = group.First().FIRM_ID,
FIRM_NAME = group.First().FIRM_NAME,
MANAGER_ACCOUNTING_CLASS_ID = group.First().MANAGER_ACCOUNTING_CLASS_ID,
MANAGER_ACCOUNTING_CLASS_NAME = group.First().MANAGER_ACCOUNTING_CLASS_NAME,
MANAGER_FUND_ID = group.First().MANAGER_FUND_ID,
MANAGER_FUND_NAME = group.First().MANAGER_FUND_NAME,
MANAGER_FUND_OR_CLASS_ID = group.First().MANAGER_FUND_OR_CLASS_ID,
NAV = group.First().NAV,
Percent = group.First().Percent,
MANAGER_STRATEGY_ID = group.First().MANAGER_STRATEGY_ID,
EMV = group.Sum(x => x.EMV),
USD_EMV = group.Sum(x => x.USD_EMV),
MANAGER_STRATEGY_NAME = group.First().MANAGER_STRATEGY_NAME,
PRODUCT_ID = group.First().PRODUCT_ID,
PRODUCT_NAME = group.First().PRODUCT_NAME })
.ToList();

Include(..) statement ignored as soon as Projections used

I have a method where (artist and genre) throws null reference exception, when i use select clause. if i don't use select clause then it can't convert system.collections.generic.list to system.collections.generic.ienumerable.
public ActionResult Attending()
{
var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
var gigs = _context.Attendances.Include(a => a.Gig.Artist).Include(a => a.Gig.Genre).Where(a => a.AttendeeId == userId).ToList();
var viewModel = new GigsViewModel()
{
UpcomingGigs = gigs,
ShowActions = User.Identity.IsAuthenticated,
Heading = "Gigs I'm Attending"
};
return View("Gigs", viewModel);
}
Here is my ViewModel:
public class GigsViewModel
{
public IEnumerable<Gig> UpcomingGigs { get; set; }
public bool ShowActions { get; set; }
public string Heading { get; set; }
}
Here is my Attendance class
public class Attendance
{
public Gig Gig { get; set; }
public ApplicationUser Attendee { get; set; }
[Key]
public int GigId { get; set; }
[Key]
public string AttendeeId { get; set; }
}

Could not find an implementation of the query pattern for source type 'DbSet<Medida>'. 'Select' not found

I have a dbcontext with a dbset, and I need to make a linq query easily.
using (ZigBeeContext db = new ZigBeeContext())
{
var lista = (from p in db.Medidas
select new Medida
{
Fecha = p.FechaHora,
}).ToList();
}
However I get the error on the title on db.Medidas
Medidas.cs
public class Medida
{
[Key]
[DisplayName("Medida")]
public int MedidaID { get; set; }
public decimal Temperatura { get; set; }
public int Humedad { get; set; }
public DateTime FechaHora { get; set; }
[DisplayName("Dispositivo")]
public int DispositivoID { get; set; }
public virtual Dispositivo Dispositivo { get; set; }
}

Categories