-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I map a Database View? #4561
Comments
@divega @rowanmiller , sorry for pinging you on Sunday, but it's quite urgent, I was asked to lecture in a meetup of several early adopting companies about ASP.NET 5 and EF 7, and it should take place this Wednesday... |
Mapping Views is not currently supported, and in the Backlog: #827 |
Thanks @ErikEJ. I'm a bit shocked... @rowanmiller is there any workaround available? Thanks. |
You can tell EF it is a table. Obviously migrations won't create a view for you, but I'm guessing you are mapping to an existing database anyway. If not an existing database then just delete the Alternatively, you can just use a raw SQL command to select from a view ( |
Thanks @rowanmiller, the trick with the "table" worked. |
Can you explain the trick? |
|
@rowanmiller Sorry I should have been more clear. What do you mean by "tell EF it is a table"? |
@jlmelis just write the code the same as you would when you map to a table. Create an entity, and if the table name it expects by convention doesn't match the name of your view then use DataAnnotations or the Fluent API to specify the "table" name. |
Steps:
If you are using migrations Note that it's a hack and very annoying when you change the View since you need to repeat all these steps on every change.😢 |
Found out that you can ignore the view table in migrations by using the model builder. But you must only do that when migration is ongoing. (IsMigration Property is defined by myself)
|
@audacity76 Any chance you can share your code or some guidance as to how you're handling IsMigration? |
@villecoder I added the IsMigration property as static to the DbContext. Default value is set to true. In my startup.cs File in the Configure method I set the property to false because the migration doesn't hit the method. I'm sure there is a better way but I didn't found one. Maybe someone here? @rowanmiller |
@audacity76 that looks like a reasonable approach. The other option is to let it scaffold the CreateTable call and then just delete it from the scaffolded migration. EF won't try and re-create it since it uses the model snapshot to diff against (rather than the actual schema). The downside is that it would try and migrate and changes you make to the view model, so you may have to delete future migration operations too. |
@rowanmiller the problem with the other approache is when deleting the snapshot file and all the migrations (a good approach IMHO for every major version) all those tricks will be lost. |
@audacity76 I think the inverted approach would be better - you set the default to false and configure it to true only for migrations. When you do Enable-Migrations, a Configuration.cs class is added to a Migrations folder and you can set your static property from there. If your migration configuration and entities are in the same project - even better - you can declare the IsMigration internal and keep it hidden from the rest of the world. |
I'm involved in a large line-of-business application and we decided to give efcore a try. And daily we regret our choice. I understand the team's desire to create a database agnostic tool. But my guess is that 90% of ef core users are running against MS SQL Server and we want all those SQL server or relational database specific feature that the team does not want to implement. I feel that ef core is ending up as being useless for all, since it won't commit to what it's supposed to be good at. If you are using relational data I feel that EF 6 and model-first is the only way to go if you want full control over your data. Code first sounds tempting, but how many hours do we need to spend on coaxing the framework to create the table we want it to. |
@paaland thanks for providing this feedback. Could you name specifically what SQL Server or general relational features you want that EF Core doesn't allow you to use? Is it just mapping to views or something else? (If the latter it might make sense to create a new issue. We don't often revisit already closed issues like this one) Also can you explain how EF6 model-first is giving you more control to create the database you want? I would really want to understand if there is anything we can do to reduce the friction and your feedback can help us prioritize future work. |
@divega I can't speak for @paaland but I would like to see Views in EF Core, (and EF6 if any work is continuing there) ; also indices with include columns. The underlying problem is performance. Using materialised indexed views can be a great way to improve performance - if only EF could allow this using Code First. I would also like read-only tables (where you can only update in the seed method). We get around some of these issues in EF6 using Sql statements in Up/Down migrations - but it feels hackish - it would be nice to have built-in support for these features. |
@tombrown571 as I mentioned in my previous comment, we don't look often at already closed issues like this one. I would recommend you search for existing issues on the areas you care about and then +1 on them (I believe there are existing issues in our backlog for the majority of the things you mentioned). |
I have an hierarchy table that I'm trying to map with EF 7.
I tried almost everything (except probably the right thing) to map the view I already create in the DB but couldn't make it to work.
This is my model:
And this is the view I'm trying to map:
What do I need to write in the DbContext to map
HierarchyPosts
?BTW How do I write custom SQL as migration script, for stuff that EF currently doesn't support such as views or for stuff that EF will never support such as partitioning.
BTW2, it would be very helpful if EF 7 would finally be able the handle recursive data natively without requiring views or SP .
Thanks
The text was updated successfully, but these errors were encountered: