19

How can you map an entity to a database view with EF 4 code first?

Is there a way to do this by deriving from the EntityConfiguration classes?

1
  • Can you futher explain your question? What is the view? Is it entity? If not how it differs from entity? Commented Aug 29, 2010 at 17:41

1 Answer 1

23

Yes! I found the answer:

You can certainly use Code First to map to a view, just tell Code First that it's a table and it will use the same SQL against the view that it would for a table. Obviously if your view isn't writeable then saving is going to fail if you try and update values in the entities that are based on the view, but we will just delegate to the database so if you don't update these entities then you won't have any issues.

http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/f154595c-717e-4703-a81d-ee63633a481e

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

7 Comments

Nice I didn't know about that but still it is a hack not a feature.
Is it really a hack? How do you distinguish between a view and a table when writing standard TSQL
@mjmcloug There is some hackery here. If we have EF create the DB, how do we tell it not to create the table? I'm resorting to creating a table the dropping it and creating a view when seeding.
Presumably, if you're trying to map a model to a view, you're talking about an existing database. Although "code first" may be a confusing name when talking about existing database, you can use it with an existing database.
I like calling Code First with an Existing Database "Code Second"
|

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.