3

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?

1
  • Not out of the box, but there are third party tools that allow you to mix both models. One tool that can do that (bring individual changes across selectively) is the EFv4 'Model Comparer' in my add-in which you can download and get a trial license for at huagati.com/dbmltools . This blog post describes the model comparer in more detail (with a screencast video showing the basic features): huagati.blogspot.com/2010/07/… Commented Mar 1, 2011 at 9:45

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it looks like using the ExecuteStoreQuery() with a custom class will do the trick for me.
1

Are you aware of the SqlQuery() method?

ctx.Listings.SqlQuery("SQL Query Here", p1, p2...)

5 Comments

Yes it is. It will run a custom query that returns rows for your table (in my case the Listings table) and then maps them into objects with all the change tacking etc turned on.
Do you have a more detailed example to show? So far I have not been able to find any information about this SqlQuery() method. What namespace does it belong to?
It is part of the DbSet<TEntity> class. So no namespace or anything needed, it is not an extension method. http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-released.aspx
Actually I may have mixed up Model First and Code First. Sorry. This method is part of the new Code First CTP bits for EF.
This method also exists in common EF but it is called ObjectContext.ExecuteStoreQuery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.