I am architechting my application on the lines of Repository pattern, Aggregate root and Unit of work. I am using Entity Framework5 as ORM.
I am in a situation where I don't know how to proceed with adding new entities that are aggregate roots and have a foreign key relation to entities that are not aggregate roots.
I am not sure if I made sense with my previous statement, but let me put it in a example here.
Person Table in Db (This is an aggregate root in my application)
- Name
- Address Line 1
- CountryID
Countries Table (This is not an aggregate root as I might never need to query them independently)
- CountryID
- Country Name
In my application I have PersonsRepository which implements a generic Repository where T is a Aggregate root.
Now when I create a new person in my code I need to add Country to the Person Object's Navigation Property. If I create a new Country Object and assign it to Person's Country property and try to save Person object the EF throws error. I can't query the Countries table because it is not my Aggregate root.
Well, this is not my actual scenario but this is what i am trying to overcome. How should I proceed from here?
One thought that comes to my mind is to create a generic read-only repository that will be used for querying the DB and not modifying it, Is it a good way to proceed or is there something wrong I am doing.
Thanks in advance for your reply and for reading long post.