child relationship not available in parent object - asp.net-core

I have the following method that returns a list of factory cars.
It works, but the ordering is wrong.
CarEngines can have an orderId and I want to order by that.
Looking at other answers on here, I see that you can't do an order by inside the query and you have to do it afterwards.
The problem is, I can't access CarEngines as you can see below:
public async Task<ActionResult<CountryList>> GetCountryCarObject(Guid countryID)
{
var factoryCars = await _context.CountryList
.Include(n => n.CarList).ThenInclude(l => l.CarEngines)
.Include(n => n.CarList).ThenInclude(l => l.CarOptions)
.SingleOrDefaultAsync(c => c.CountryId == countryID);
factoryCars.CarList.CarEngines <== CarEngines doesn't show up in CarList object
return factoryCars;
}
It is telling me that CarList doesn't contain a definition for CarEngines.
But it is in my CarList model, I have it defined like so:
public CarList()
{
CarEngines = new HashSet<CarEngines>();
}
public virtual ICollection<CarEngines> CarEngines { get; set; }
Here are the two models:
public partial class CarList
{
public CarList()
{
CarEngines = new HashSet<CarEngines>();
CarOptions = new HashSet<CarOptions>();
}
public string CarId { get; set; }
public string CarMake { get; set; }
public string CarModel { get; set; }
public Guid? CarCountryId { get; set; }
public virtual ICollection<CarEngines> CarEngines { get; set; }
public ICollection<CarOptions> CarOptions { get; set; }
}
public partial class CountryList
{
public CountryList()
{
CarList = new HashSet<CarList>();
}
[Key]
public Guid CountryId { get; set; }
public string CountryName { get; set; }
public string CountryLocation { get; set; }
public string CountryDesc { get; set; }
public virtual ICollection<CarList> CarList { get; set; }
}
So I'm not sure it doesn't see it.
Is there a way to get this to work?
Thanks!

Ok so the fact that there is something called CarList but is not a List is super confusing but moving on....
The issue is that CarList is a List. So use something like factoryCars.CarList.Select( x=>x.CarEngines). Also rename that to var country instead of var factoryCars since you return a single country and not a list of cars.
Also rename your variables and classes this confusion was probably caused by this. For example instead of having ICollection<CarList> CarList you can rename it into ICollection<Car> Cars so right now from the name you can easilly understand there are multiple cars (thus its a collection) which includes the object Car

Related

Linq not seeing properties

I have the following class and I'm trying to access it's properties from a different related class as follows:
var nuInfo = recipe.RECIPE_INGREDIENT
.Select(i => i.INGREDIENT.INGREDIENT_NUTRITIONAL_INFO)
.Where(ni => ni.NUTRITIONAL_INFO.Main == 1);
However, I can't access any (virtual or not) properties of INGREDIENT_NUTRITIONAL_INFO.
The INGREDIENT_NUTRITIONAL_INFO class is as follows:
public class INGREDIENT_NUTRITIONAL_INFO
{
public int IngredientId { get; set; }
public int Nutritional_InfoId { get; set; }
public double Amount { get; set; }
public DateTime DateSubmitted { get; set; }
public DateTime DateModified { get; set; }
public string SubmittedBy { get; set; }
public string ModifiedBy { get; set; }
public virtual AspNetUsers AspNetUsers { get; set; }
public virtual AspNetUsers AspNetUsers1 { get; set; }
public virtual INGREDIENT INGREDIENT { get; set; }
public virtual NUTRITIONAL_INFO NUTRITIONAL_INFO { get; set; }
}
Error Code is as follows:
CS1061 'ICollection' does not contain a definition for 'NUTRITIONAL_INFO' and no extension method 'NUTRITIONAL_INFO' accepting a first argument of type 'ICollection' could be found (are you missing a using directive or an assembly reference?)
Am I missing something in Linq? Am I trying to traverse across too many relationships?
The error is because you're not adding .First() or .FirstOrDefault() on the end of your query:
var nuInfo = recipe.RECIPE_INGREDIENT.Select(i => i.INGREDIENT.INGREDIENT_NUTRITIONAL_INFO)
.Where(ni => ni.NUTRITIONAL_INFO.Main == 1)
Your code is trying to access NUTRITIONAL_INFO as a property of the collection, not as the property of a member in the collection.
The problem is that nuInfo is not just one INGREDIENT_NUTRITIONAL_INFO object, your LINQ query returns a result as a IEnumerable<T>.
If you want to get a single result you can use First(),FirstOrDeafult, Single() or SingleOrDefault to instead return a single result.
var nuInfo = recipe.RECIPE_INGREDIENT
.Select(i => i.INGREDIENT.INGREDIENT_NUTRITIONAL_INFO)
.FirstOrDefault(ni => ni.NUTRITIONAL_INFO.Main == 1);

MVC with properties not mapped to the database

I'm creating a view with a drop list and some other fields that will update a database. In the model there are properties that map to the database and some properties that are used for the dropdownlistfor. The unmapped properties throws an exception. Is there a good way to exclude the drop list properties from being mapped? I tried putting them in a separate class in the model and that didn't work.
The model:
[Table("cardata")]//Links the external table to this model object
public class Cardata
{
//Maps to the database
public int id { get; set; }
public int dealerID { get; set; }
public string model { get; set; }
public int numCyl { get; set; }
public double weight { get; set; }
// UNMAPPED Used for a drop list of car names
public string carModel { get; set; }
public IEnumerable<SelectListItem> carList
{
get
{
cartableContext ctc = new cartableContext();
IEnumerable<SelectListItem> retVal = ctc.cardata.GroupBy(c => c.model).Select(cl => cl.FirstOrDefault()).Select(cars => new SelectListItem { Value = cars.id.ToString(), Text = cars.model.ToString() });
return retVal;
}
set { }
}
}
Could you use [NotMapped]?
[NotMapped]
public string carModel { get; set; }
[NotMapped]
public IEnumerable<SelectListItem> carList{...}

How to use Raven LoadDocument

I'm having trouble querying RavenDB with even the simplest of queries, probably I'm doing something wrong, but after a few hours I just can't see it anymore. I've googled almost anything I can think of..
I have these entities:
public class User
{
public string Id { get; set; }
public string DisplayName { get; set; }
public string RealName { get; set; }
public string Email { get; set; }
public string PictureUri { get; set; }
public List<Comment> Comments { get; set; }
public List<Role> Roles { get; set; }
}
public class NewsItem
{
public string Id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public DateTime Created { get; set; }
public string UserId { get; set; }
public List<Tag> Tags { get; set; }
public List<Comment> Comments { get; set; }
public List<WebImage> Images { get; set; }
}
I want to query these so I get a list of newsItems, but with the user information alongside it. So I read the docs and tried the LoadDocument feature, the index:
public class NewsItemIndexWithComments : AbstractIndexCreationTask<NewsItem, NewsItemIndexWithComments.Result>
{
public class Result
{
public string AuthorName { get; set; }
}
public NewsItemIndexWithComments()
{
Map = newsItems => from newsItem in newsItems
select new
{
AuthorName = LoadDocument<User>(newsItem.UserId).DisplayName
};
}
}
Which I try to use like:
var result = _documentSession.Query<NewsItemIndexWithComments.Result, NewsItemIndexWithComments>().AsProjection<NewsItemIndexWithComments.Result>().ToList();
Now I get the number of documents in my list, but the AuthorName is always null. If I don't use the AsProjection method, I won't get any results. Can anyone show me a proper example on which I can experiment further?
Thanks.
_ edit:
That helped a lot, thanks :) Now for step two, I'm sorry if I'm being a bit newbish, but you'll have to start somewhere. In the newsitems there are comments, in these comments there is another reference to the userid. You can probably guess what I want to do: I want the user info for the comments with the comments as well.
new Index:
public class NewsItemIndexWithComments : AbstractIndexCreationTask<NewsItem, NewsItemIndexWithComments.Result>
{
public class Result : NewsItem
{
public string AuthorName { get; set; }
public string AuthorId { get; set; }
}
public NewsItemIndexWithComments()
{
Map = newsItems => from newsItem in newsItems
let user = LoadDocument<User>(newsItem.UserId)
select new
{
AuthorName = user.DisplayName,
AuthorId = user.Id,
};
Store(x => x.AuthorName, FieldStorage.Yes);
Store(x => x.AuthorId, FieldStorage.Yes);
}
}
Comment class:
public class Comment
{
public string Id { get; set; }
public string Text { get; set; }
public string UserId { get; set; }
public string User { get; set; }
public DateTime Created { get; set; }
}
How can I query the comments and expand the results for that? Or is it better to create a new index just for the comments and get the user info analog to the solution above?
You're almost there, you just need to store the field you are projecting. Add this to the index constructor, after the map.
Store(x=> x.AuthorName, FieldStorage.Yes);
This is because you want it returned and available for AsProjection to find. If you just wanted to use the author name in a where or orderby, you wouldn't need it.
If you just want to include the comments in your AsProjection, you can simply index the entire object along.
Note that indexing a custom object will mean that you're not able to query on it using a .Where(). RavenDB can only query on flattened results (ints, decimals, strings, dates).
In order to, for instance, query on the title, you will need to create a seperate Property public string Title { get; set; } and map it with Title = newsItem.Title.
public class NewsItemIndexWithComments : AbstractIndexCreationTask<NewsItem, NewsItemIndexWithComments.Result>
{
public class Result : NewsItem
{
public string AuthorName { get; set; }
public string AuthorId { get; set; }
public List<Comment> Comments { get; set; }
}
public NewsItemIndexWithComments()
{
Map = newsItems => from newsItem in newsItems
let user = LoadDocument<User>(newsItem.UserId)
select new
{
AuthorName = user.DisplayName,
AuthorId = user.Id,
Comments = newsItem.Comments.
};
Store(x => x.AuthorName, FieldStorage.Yes);
Store(x => x.AuthorId, FieldStorage.Yes);
Store(x => x.Comments, FieldStorage.Yes);
}
}

CodeFirst - Update single property

We are using EF5, Code First approach to an MVC4 app that we're building. We are trying to update 1 property on an entity but keep getting errors. Here's what the class looks like which the context created:
public partial class Room
{
public Room()
{
this.Address = new HashSet<Address>();
}
public int RoomID { get; set; }
public Nullable<int> AddressID { get; set; }
public Nullable<int> ProductVersionID { get; set; }
public string PhoneNumber { get; set; }
public string AltPhone { get; set; }
public string RoomName { get; set; }
public string Description { get; set; }
public string Comments { get; set; }
public string Notes { get; set; }
public virtual ICollection<Address> Address { get; set; }
}
Here's our ViewModel for the view:
public class RoomDetailsViewModel
{
//public int RoomID { get; set; }
public string RoomName { get; set; }
public string PhoneNumber { get; set; }
public string AltPhone { get; set; }
public string Notes { get; set; }
public string StateCode { get; set; }
public string CountryName { get; set; }
public string ProductVersion { get; set; }
public int PVersionID { get; set; }
public List<SelectListItem> ProductVersions { get; set; }
public Room Room { get; set; }
}
Here's the Controller Action being called on "Save":
[HttpPost]
public virtual ActionResult UpdateRoom(RoomDetailsViewModel model)
{
var db = new DBContext();
bool b = ModelState.IsValid;
var rooms = db.Rooms;
var rm = rooms.Where(r => r.RoomID == model.Room.RoomID).Single();
//List<Address> address = db.Addresses.Where(a => a.AddressID == rm.AddressID).ToList<Address>();
rm.ProductVersionID = model.PVersionID;
//rm.Address = address;
db.Entry(rm).Property(r => r.ProductVersionID).IsModified = true;
//db.Entry(rm).State = System.Data.EntityState.Modified;
db.SaveChanges();
return View("RoomSaved", model);
}
All this view does is display data and allow the user to change the Product Version (from a SelectList), so, in the Room Entity, all we are updating is the ProductVersionID property, nothing else. We can get the data to display properly but when we click "save", we get this error:
An object of type 'System.Collections.Generic.List`1[[Models.Address,
Web.Mobile.TestSite, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]' cannot be set or removed from the Value
property of an EntityReference of type 'Models.Address'.
As you can see by the Controller Action, we've tried several different things but all seem to produce this error. I've tried to populate the model.Room.Address collection with an Address, without, but still get this error.
I read this StackOverflow article and this article as well but neither have solved my problem.
ANY help with this would be greatly appreciated!
After hours and hours of digging, turns out that EF did not import some of the PK's for my DB tables. What tipped me off to this was on the Room class, the PK RoomID did not have the [Key] attribute on it. I tried to reimport the table through the edmx but it never came through as a key (even though it's clearly marked PK in the DB). So, to get around it, I created a partial class of my DBContext and override the OnModelCreating event and included the key, like so:
public partial class DBContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Models.Room>().HasEntitySetName("Rooms");
modelBuilder.Entity<Models.Room>().HasKey(r => r.RoomID);
}
}
Once this was done, the Action saved the record as hoped.
I hope this helps someone else!

RavenDB TransformResults

I'm trying to use the TransformResults feature, and I can't get it to work. I'm not totally sure I understand this feature, perhaps there is another way to solve this problem. What I want is just the Id from the Order and the email addesses from the Customer and the Entrepreneur. I am happy for all tips that can take me in the right direction. Here is my code.
Document
public class OrderDocument
public string Id {get; set }
public EntrepreneurInfo EntrepreneurInfo { get; set; }
public CustomerInfo CustomerInfo { get; set; }
public OrderStatus CurrentOrderStatus { get; set; }
}
Info classes
public class EntrepreneurInfo
{
public string EntrepreneurDocumentId { get; set; }
public string Number { get; set; }
public string Name { get; set; }
}
public class CustomerInfo
{
public string CustomerDocumentId { get; set; }
public string Number { get; set; }
public string Name { get; set; }
}
The info classes are just subsets of a Customer and Entrepreneur documents respectively.
The Customer and Entrepreneur documents inherits from a base class ( AbstractOrganizationDocument) that has the EmailAddress property.
My Index
public class OrdersApprovedBroadcastingData :
AbstractIndexCreationTask<OrderDocument, OrdersApprovedBroadcastingData.ReduceResult>
{
public OrdersApprovedBroadcastingData()
{
this.Map = docs => from d in docs
where d.CurrentOrderStatus == OrderStatus.Approved
select new
{
Id = d.Id,
CustomerId = d.CustomerInfo.CustomerDocumentId,
EntrepreneurId = d.EntrepreneurInfo.EntrepreneurDocumentId
};
this.TransformResults = (db, orders) => from o in orders
let customer = db.Load<CustomerDocument>(o.CustomerId)
let entrepreneur = db.Load<EntrepreneurDocument>(o.EntrepreneurId)
select
new
{
o.Id,
o.CustomerId,
CustomerEmail = customer.EmailAddress,
o.EntrepreneurId,
EntrepreneurEmail = entrepreneur.EmailAddress
};
}
public class ReduceResult
{
public string Id { get; set; }
public string CustomerId { get; set; }
public string CustomerEmail { get; set; }
public string EntrepreneurId { get; set; }
public string EntrepreneurEmail { get; set; }
}
}
If I look at the result of this Index in Raven Studio I get null values for all fields except the Id. And finally here is my query.
Query
var items =
this.documentSession.Query<OrdersApprovedBroadcastingData.ReduceResult, OrdersApprovedBroadcastingData>()
.Select(x => new OrdersToBroadcastListItem
{
Id = x.Id,
CustomerEmailAddress = x.CustomerEmail,
EntrepreneurEmailAddress = x.EntrepreneurEmail
}).ToList();
Change your index to:
public class OrdersApprovedBroadcastingData : AbstractIndexCreationTask<OrderDocument>
{
public OrdersApprovedBroadcastingData()
{
Map = docs => from d in docs
where d.CurrentOrderStatus == OrderStatus.Approved
select new
{
};
TransformResults = (db, orders) =>
from o in orders
let customer = db.Load<CustomerDocument>(o.CustomerInfo.CustomerDocumentId)
let entrepreneur = db.Load<EntrepreneurDocument>(o.EntrepreneurInfo.EntrepreneurDocumentId)
select new
{
o.Id,
CustomerEmailAddress = customer.EmailAddress,
EntrepreneurEmailAddress = entrepreneur.EmailAddress
};
}
}
Your result class can simply be the final form of the projection, you don't need the intermediate step:
public class Result
{
public string Id { get; set; }
public string CustomerEmailAddress { get; set; }
public string EntrepreneurEmailAddress { get; set; }
}
You don't have to nest this class in the index if you don't want to. It doesn't matter either way. You can query either with:
var items = session.Query<Result, OrdersApprovedBroadcastingData>();
Or with
var items = session.Query<OrderDocument, OrdersApprovedBroadcastingData>().As<Result>();
Though, with the first way, the convention tends to be to nest the result class, so really it would be
var items = session.Query<OrderDocument.Result, OrdersApprovedBroadcastingData>();
Note in the index map, I am not including any properties at all. None are required for what you asked. However, if you want to add a Where or OrderBy clause to your query, any fields you might want to filter or sort on should be put in there.
One last thing - the convention you're using of OrderDocument, CustomerDocument, EntrepreneurDocument, is a bit strange. The usual convention is just Order, Customer, Entrepreneur. Think of your documents as the persisted form of the entities themselves. The convention you are using will work, it's just not the one usually used.