How map objects to a view with EF 4 code first?
Asked Answered
C

1

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?

Chaves answered 29/8, 2010 at 17:24 Comment(1)
Can you futher explain your question? What is the view? Is it entity? If not how it differs from entity?Jaunitajaunt
C
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

Chaves answered 30/8, 2010 at 15:50 Comment(7)
Nice I didn't know about that but still it is a hack not a feature.Jaunitajaunt
Is it really a hack? How do you distinguish between a view and a table when writing standard TSQLTormentor
@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.Scrawny
Sorry @Scrawny I don't use EF to create the database so can't help you on that one.Tormentor
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.Samaveda
I like calling Code First with an Existing Database "Code Second"Psephology
"you're talking about an existing database" What about a view created to work around a EF6 incapability with multiple projections and union?Dennett

© 2022 - 2024 — McMap. All rights reserved.