For my first project using Entity Framework, I chose to use the "model-first" approach wherein I design my entities from which a script will be created for generating the required database tables. This has worked quite well 开发者_如何学编程until I ran into a situation where I couldn't quite retrieve all the data elements I am needing in a single query (my LINQ skills are still limited).
Since I can easily write the query I need in SQL, I was wondering whether it would be possible to write a view for my database and then generate an entity in my model from it, in other words, mixing the two model/database approaches. Any ideas on this?
You can't mix model-first and db-first. Once you manually modify database you can't use Generate database from EDMX any more or you delete your changes performed directly in DB.
In some cases it can be avoided by downloading Entity Designer Database Generation Power Pack extension to Visual Studio 2010. When using this extension together with VS 2010 Premium or Ultimate you can use additional DB generation workflows and T4 templates which are able to use VS tools to compare newly generated DB with existing DB and create only ALTER scripts.
But still this will most probably not work with DB views because information about DB view is stored in SSDL (storage model description). Model-first doesn't use Views and every time you regenerate database it creates table instead of view.
So if you want to run arbitrary SQL query use ExecuteStoreQuery (only EF4) or give up with model first.
Are you aware of the SqlQuery() method?
ctx.Listings.SqlQuery("SQL Query Here", p1, p2...)
精彩评论