C
C#16mo ago
FR

❔ Entity Framework, How To Join Second Table To Third Table In The First Table?

Hi i want to join table city to area like picture below, but i got an error like this
System.ArgumentException: The properties expression 'ab => Convert(ab.City.AreaId, Object)' is not valid. The expression should represent a simple property access: 't => t.MyProperty'. When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'. (Parameter 'propertyAccessExpression')
System.ArgumentException: The properties expression 'ab => Convert(ab.City.AreaId, Object)' is not valid. The expression should represent a simple property access: 't => t.MyProperty'. When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'. (Parameter 'propertyAccessExpression')
anyone know how to do it properly? im only know to join the first and second table and still learning ty.
7 Replies
Angius
Angius16mo ago
Just so you know, builder.Property(x => x.Prop) without actually configuring that property is useless Also, how do your models look? The error, of course, is that a foreign key has to be a property Not a property of an object So ab.City.AreaId is not valid
FR
FROP16mo ago
welp i think i could cheat like that xD it was a simple model like this
public class AreaBranch
{
public long Id {get; set;}
public long CityId {get; set;}
xxx property
public virtual City {get; set;}
public virtual Area {get; set;}
}
public class City
{
public long Id {get; set;}
public long AreaId {get; set;}
xxx property
}
public class Area
{
public long Id {get; set;}
xxx property
}
public class AreaBranch
{
public long Id {get; set;}
public long CityId {get; set;}
xxx property
public virtual City {get; set;}
public virtual Area {get; set;}
}
public class City
{
public long Id {get; set;}
public long AreaId {get; set;}
xxx property
}
public class Area
{
public long Id {get; set;}
xxx property
}
Angius
Angius16mo ago
Ah, a many-to-many relationship with additional data on the pivot table, basically? Uh, kinda Because City and Area also have a direct relationship
FR
FROP16mo ago
yeah prob like that, Area it should be Provience but i forgot to change the name my man, i accidentally found it! just change my linq query from this
.Select(areaBranch => new AreaBranch
{
Id = areaBranch.Id,
BranchId = areaBranch.BranchId,
CityId = areaBranch.CityId,
Name = areaBranch.Name,
Status = areaBranch.Status,
City = areaBranch.City,
})
.Select(areaBranch => new AreaBranch
{
Id = areaBranch.Id,
BranchId = areaBranch.BranchId,
CityId = areaBranch.CityId,
Name = areaBranch.Name,
Status = areaBranch.Status,
City = areaBranch.City,
})
to this
_persistence.AsQueryable()
.Select(areaBranch => new AreaBranch
{
Id = areaBranch.Id,
BranchId = areaBranch.BranchId,
CityId = areaBranch.CityId,
Name = areaBranch.Name,
Status = areaBranch.Status,
City = areaBranch.City,
Area = areaBranch.City.Area
})
.ToList());
_persistence.AsQueryable()
.Select(areaBranch => new AreaBranch
{
Id = areaBranch.Id,
BranchId = areaBranch.BranchId,
CityId = areaBranch.CityId,
Name = areaBranch.Name,
Status = areaBranch.Status,
City = areaBranch.City,
Area = areaBranch.City.Area
})
.ToList());
also add prop public virtual Area { get; set; } in City Model and remove Join Area in AreaBranch Builder here some result from my JSON
{
"branchId": "DF",
"cityId": 1,
"name": "Default",
"status": false,
"city": {
"cityCode": "default_in",
"cityName": "default_in",
"areaId": 11,
"area": {
"name": "default_in",
"cityName": "default_in",
"status": false,
"countryId": "IN",
"parentId": null,
"id": 11
},
"id": 1
},
"area": {
"name": "default_in",
"cityName": "default_in",
"status": false,
"countryId": "IN",
"parentId": null,
"id": 11
},
"id": 1
}
{
"branchId": "DF",
"cityId": 1,
"name": "Default",
"status": false,
"city": {
"cityCode": "default_in",
"cityName": "default_in",
"areaId": 11,
"area": {
"name": "default_in",
"cityName": "default_in",
"status": false,
"countryId": "IN",
"parentId": null,
"id": 11
},
"id": 1
},
"area": {
"name": "default_in",
"cityName": "default_in",
"status": false,
"countryId": "IN",
"parentId": null,
"id": 11
},
"id": 1
}
idk why if i exclude Area = areaBranch.City.Area from my linq, my City didnt want to show up Area, but hei if its work its work. anyway ty that was fast
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX16mo ago
Use the /close command to mark a forum thread as answered
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server