How to have left outer join with entity consisting of 2 tables?
We are having two tables named T1 and T2 in our database. We want to map these two tables(T1,T2) to single entity(E1) on entity framework. The relationship between T1 and T2 is not One-to-one. The T2 table may or may not have records in T1. But the reverse is always true.
If we add these 2 tables in single entity then by default we get ‘Inner join’ (If we attach profiler we can check that) when the SQL query is fired on the DB.
We want the query to form the Left Outer join so as to get the expected result. How can we achieve the same?
We have considered option to create view on T1 and T2 tables and map it to our entity. But we cannot use view to carry out DML operation (insert,update,delete).
Is there any other alternative available which can satisfy our above requirement other than using view ?
Answers
Hi Kinnar,
When you create a FK between the two table, the generated association will be 1 to 0.1.
If it is that case, you can use the function to achieve it to do left outer join.Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.
Best Regards
Yichun Feng
- Edited byYichun_FengMSFT, ModeratorWednesday, November 18, 2009 9:09 AM
- Marked As Answer byYichun_FengMSFT, ModeratorFriday, August 28, 2009 3:31 AM
All Replies
Hi Kinnar,
For your issue, I think it is better for you to use table per type inheritance.
First, mapping multiply tables into one entity is for the tables that share the common primary key. The inner join feature is based on that prerequisite.
Second, with table per type inheritance, you can also insert, update, and delete records cascadingly, as long as you have foreign key constraints on the table.
Here is a blog talking about table per type inheritance:
This MSDN document is about add, updating and delete objects:
http://msdn.microsoft.com/en-us/library/bb738695.aspx
Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.
Best Regards
Yichun Feng
- Unmarked As Answer byYichun_FengMSFT, ModeratorFriday, August 21, 2009 5:50 AM
- Marked As Answer byLingzhi SunMSFT, ModeratorThursday, August 20, 2009 4:07 AM
- Hi Yichun,
Thanks for your reply. Both the tables in our database having same primary key. But one of table has some record which others table may not have. So, its not 1-to-1 strict mapping. Its like 0..1 to 1 mapping. We want the query to be have left outer join between tables rather taking "inner join" implicitly.
Can we achieve this ? Hi Kinnar,
When you create a FK between the two table, the generated association will be 1 to 0.1.
If it is that case, you can use the function to achieve it to do left outer join.Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.
Best Regards
Yichun Feng
- Edited byYichun_FengMSFT, ModeratorWednesday, November 18, 2009 9:09 AM
- Marked As Answer byYichun_FengMSFT, ModeratorFriday, August 28, 2009 3:31 AM
- Hi Yichun,
I have the same problem, but some thing more.
What happends if I want to order the query by CustomerAddress.PostCode. It says that is a list and can't order it.
Thanks - Hi Fiege,
Could you post a new thread for this question and give more details about it?
In my sample code, my two tables have 1:1 association. In DB, there is a Fk between Customer.Id and CustomerAddress.CustomerId.
If you want using 1:M assocation, CustomerAddress.PostCode is collection.
Best Regards
Yichun Feng
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


