I am creating a new project from scratch. I created db, where I consistently applied the db structure, which I explain with a short explanatory example:
Table element β (Id, Name) β Contains general information
Table ItemInfo β (Item_Id, Language, Description) β Contains language-specific information.
Id and Item_Id are associated with a foreign key relationship.
My idea was to model it in such a way that in the end I would use only one POCO "Item" populated through the Entity Framework. This object will contain only public properties: Id, Name and Description. The language will be hidden from the code using this object, the object itself should be responsible for the correct description depending on the global variable containing this language.
I tried several ways to do this and always got problems because the Entity Framework did not allow this scenario. I always had to extract information for ALL languages, and not just for the current one or use 2 different queries.
So, at the end, the solution I started using was to allow the T4 template to create both Item and ItemInfo, and then I manually added code that looked like this:
public partial class Item { private ItemInfo _itemInfo = null; private ItemInfo itemInfo { get { if (_itemInfo == null) _itemInfo = ItemInfoes.Single(p => p.Language == GlobalContext.Language); return _itemInfo; } } public Description { get { return itemInfo.Description; } set { itemInfo.Description = value;} } }
Using this code, I added additional properties from ItemInfo to Item and chose the correct language according to my requirements. Do you think this is a good solution? How would you solve this problem instead?
However, running the sql profiler, I see that two different sql queries are used to populate the Item object, one of which queries the Item table and the other that queries ItemInfo.
Is it possible to achieve the same scenario with a single query that performs a join between two tables? (I am afraid that this will lead to a long-term result, and I will also do it without ORM).
Any suggestion would be welcome, I have many years of programming experience, but I am new to Entity Framework and ORM in general.
Please, help.