Take the 2-minute tour ×

I have an edmx file that reflects 100% my DB schema. Along with this I need to create some custom Entities based on the existent, pretty much like a view that will aggregate fields of several entites into a single one.

The problem is that this isn't quite working. I tried several approaches but it always gave me conflicts with the actual entites already on the edmx.

I need to have those entities that reflect my DB schema, so do I have to create another edmx file to hold my custom entites and avoid colisions?

I also though of:

  • create a stored procedure but then if I need to filter the SP result I eather have to add support for serach on the SP of get all the rows and filter with Linq2Objects... won't do this!
  • create a View, and this one would work pretty well but I want to try to do this making use of the EF4 designer and keep everything in one place.

Could anyone point me to some examples?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You can't define two entities based on same table except special cases (table splitting, hiearchy mapping). In this case you have to use DefiningQuery as @James suggested or QueryView. The difference is that DefiningQuery is defined in storage model and it is common SQL. QueryView is defined in conceptual model and it is ESQL defined on top of already existing entities. QueryView supports only some features of ESQL (for example it doesn't support aggregate functions). In both cases you have to modify EDMX directly (XML), these features are not supported in designer.

Database view mentioned by @James is also an option if you don't want to use these advanced EF features. You can as well simply expose predefined queries on your object context and map return projection to custom type.

Be aware that neither of these methods will allow you to modify, insert or delete data.

share|improve this answer
add comment

I think what you are describing is a view so this is probably the right way to go.

You can store the view code in the edmx using the DefiningQuery node. You don't need to create the view in the database. However there is no designer support for this feature, you will need to hand edit the edmx. The changes you make should be persisted if you refresh the edmx from the database using the designer.

Details here:



It would be easier just to create the views in the database, and let the designer find and model them.

This entity will be read-only, but of course you can then assign SPs for UPDATE/INSERT/DELETE if you want to support modifications via this view.

share|improve this answer
+1 for creating the views in the database directly. Why not leverage what's already there? –  alex Feb 8 '11 at 11:56
yeah, like I said above, creating a View was my only option but I thought EF4 supported this kind of requirement and I was missing something. I don't like the idea of changing the edmx guts... it's that kind of things that will blow on your hands when you least expected. –  AlexCode Feb 8 '11 at 14:20
lol.. designers are turning us into lazy programmers. It's just a bit of xml. You don't get all the power of the EF just using the designer, and the designer has a few bugs that can blow up in any case. Knowing the xml is better all round. There are some very good schema docs on it (see CDSL specification etc linked from here - msdn.microsoft.com/en-us/library/cc982042.aspx) –  James Gaunt Feb 8 '11 at 16:53
add comment

Your Answer


