I was wondering when querying a ODATA service with LINQ if I can expand only if a value exists. I have a parent object that possess a property that is another object that possess a object collection.
Example:
class Object1
{
public int id {get; set;}
public Object2 {get; set;}
}
class Object2
{
public int id {get; set;}
public List<Object3> childCollection {get; set;}
}
My query is as so:
var data = datacontext.Object1.Expand("Object2/ChildCollection")
This fails because some of the OBJECT1 do not have a OBJECT2. Is there a way around this like conditionally expanding?
Related
I have to tables I want to Join which I can do with just EFCore but I can't figure out how to also use automapper.
So if I have two Classes
public class ItemVM {
public int Id {get; set;}
public int ItemName { get; set; }
public int GroupId {get; set;}
}
public class GroupVM {
public int Id {get; set;}
public string GroupName {get; set;}
}
And I want to end up with:
public class ItemWithGroupVM {
public int Id {get; set;}
public string Name {get; set;}
public int GroupId {get; set;}
public string GroupName {get; set;}
}
So all our db fields have prefixes so in my mapping profile I have:
RecognizePrefixes("it");
CreateMap<Item, ItemVm>();
RecognizePrefixes("gr");
CreateMap<Group, GroupVm>();
Then I Run my queries:
var items = await _dbContext.Items.ProjectTo<ItemsVM>(_mapper.ConfigurationProvider).ToListAsync();
var groups = await _dbContext.Groups.ProjectTo<GroupsVM>(_mapper.ConfigurationProvider).ToListAsync();
Then I just loop through the list of items and generate a new list of ItemsWithGroupVM and set the group name string for the final output.
How can I modify my automapper profile and ef core query to do the join and the mapping without having to loop?
I just wandering i insert my data in relation data i tried to not insert in the related model that model. the result is null and doesnt have a key save in db.
its one-to-one relation
C# ef core
i tried this in json
{
testingName: "123231",
testingRelatedData: {
testingNameRelated: "asdasd"
}
}
and code is a normal way of adding in context.
class model {
public int modelid {get; set;}
public string testingName {get; set;}
public virtual testingNameRelated testingNameRelated { get; set; }
}
class testingNameRelated{
public int id {get; set;}
public string testingNameRelated{get; set;}
public model model {get; set;}
public int modelid {get; set;}
}
context.testing.add(model)
thanks
Having the following database schema:
objects1
id
name
objects2
id
object1_id
Which translates to the following:
public class Objects1
{
public Guid Id {get; set;}
public string Name {get; set;}
public string ICollection<Objects2> Object2List {get; set;}
};
public class Objects2
{
public Guid Id {get; set;}
public string Name {get; set;}
public Objects1 Object1 {get; set;}
};
How can I filter objects1 rows according to objects2 values?
Something like:
mydb.Objects1.Where(o1 => o1.Object2List.Any(o2.Name.Contains("bla")))
But this gives a runtime exception.
So, on a very simple test, everything works, but on my case, I'm generating the OrderBy and ThenBy calls dynamically at runtime, so I need to call OrderBy or ThenBy depending on if the IQueryable is in fact a IOrderedQueryable.
Strangely, if calling something like queryable.Where(o1 => o1.Object2List.Any(o2.Name.Contains("bla"))) before ordering, var ordered = queryable as IOrderedQueryable<TModel>; is not null, but the call to ThenBy fails. I fixed the problem by ordering before filtering.
I have the following model:
public class Address
{
public int Id {get; set;}
public string Street1 {get; set;}
...
public int CountryId {get; set;}
public Country Country {get; set;}
}
public class Country
{
public int Id {get; set;}
public string Name {get; set;}
public string ISOCode {get; set;}
public string Continent {get; set;}
public string ISOCode {get; set;}
public string Languages {get; set;}
}
public class Church
{
public int Id {get; set;}
public string Name {get; set;}
public int CountryId {get; set;}
public Country Country {get; set;}
public int AddressId {get; set;}
public virtual Address Address {get; set;}
public string Phone {get; set;}
}
Does the serializer think that I have some kind of bi-directional relation going on with Country since both Church and Address have a Country object? If not, then why do I get circular reference when trying to serialize a church object?
EDIT:
What's even more confusing to me is that I'm not even including Country (on church) when I query:
var results = _context.Churches.Include(c => c.Address).Include(c => c.Address.Country).AsQueryable();
Entity Framework Context is configured so that LasyLoading is not enabled. Seems to me that Church.Country should be null and shouldn't even be an issue here.
I believe the problem lies with the fact that you're serializing EF entities, which are in fact proxies with references to DataContext. Inspect them in the debugger.
You will have to use JsonObject(MemberSerialization.OptIn) attribute and mark your properties with JsonProperty attribute. More info in documentation.
I have a table (Event) that can have 2 Locations (main, alternate). Locations can be used in other tables (so no EventId in the locationTable) Using POCO self-tracking, how do I create the reference from the Event table to the Locations table, or what is the best way to handle this situation (I'm having a total brain freeze over this)? (.NET 4.0 C#, EF4.1, MVC 3 being used).
Simplified classes:
public class Event
{
public int EventId {get; set;}
public string Tile {get; set;}
public int MainLocationId {get; set;}
public int AltLocationId {get; set;}
public virtual ICollection<Location> EventLocations {get; set;}
}
public class Location
{
public int LocationId {get; set;}
public string Name {get; set;}
}
I was thinking a linking table (EventLocations) with the PK of each table and a flag indicating if it's the main or alt location, but I'm not sure how this would look in a POCO class setup. Maybe this, but it requires extra business logic (and I ultimately would like to be able to incorporate this king of solution into a T4 so I don't have to add business logic in future projects):
public class Event
{
public int EventId {get; set;}
public string Tile {get; set;}
public virtual ICollection<EventLocation> EventLocations {get; set;}
}
public class Location
{
public int LocationId {get; set;}
public string Name {get; set;}
public virtual ICollection<EventLocation> EventLocations {get; set;}
}
public class EventLocation
{
public int EventId {get;set;}
public int LocationId {get;set;}
public bool IsMain {get; set;}
public virtual Event Event {get;set;}
public virtual Location Location {get;set;}
}
Thanks for any advice/ tips/ solutions/ constructive criticism!
The simplest way is simply using:
public class Event
{
public int EventId {get; set;}
public string Tile {get; set;}
public int MainLocationId {get; set;}
public int AltLocationId {get; set;}
public virtual Location MainLocation {get; set;}
public virtual Location AlternativeLocation {get; set;}
// You can also add Foreign key properties for locations to simplify some usage scenarios
}
public class Location
{
public int LocationId {get; set;}
public string Name {get; set;}
}
You have exact number of locations defined for your event so using many-to-many relation is probably not needed (it can accidentally add more locations to your Event).