How to add nonclustered indexes in Entity Framework db-first project? - c#

I have a table in SQL Server which includes custom nonclustered indexes for some columns. And I have also a project with below versions.
.Net Framework 4.6.2
Entity Framework 6.4.4
I added an entity data model to project based on my existing database (using a database-first approach).
Although a SELECT query returns quickly in database, the same query gets a timeout error in the project. And I thought that EF couldn't add
column indexes to the project. I searched index names in the project,
but there isn't any code about indexes in the project.
So, how can I add indexes to my database-first model?

So, how can I add indexes to my database-first model?
"database-first" means that you apply design changes to the database first, and then apply any changes to the EF model second.
So you just create the indexes in SQL Server using Visual Studio or SSMS, and since EF model doesn't need to change when you add an index, you're done.

Related

Deleting columns from Database doesn't give a mistake in project

So there's an ASP.Net project that uses Entity Framework Core. Of course, there are Entities classes, Configuration Entities classes, etc. And there are properties in Entities that map some table's columns. Also there is connection string to connect to database. The thing I don't get: I deleted some columns from database table. But project builds and everything is fine. What do I miss? I though that mistake should be given because Entity properties don't map table columns...
A successful build has nothing to do with working code. If you remove columns from the database the project builds, since the project does not have information about the database structure. But as soon as you query for the table where the columns have been deleted, then the code should throw exceptions. I suggest you not to modify the database directly, but read into the topic of EF migrations so that modifying the model updates the database.

Entity Framework Core Database First

My team has inherited a database application that contains hundreds of tables. The application uses Entity Framework and takes a database first approach for development. Our current process is to pull a table or two at a time into the edmx using the Update Model From Database... tool.
We are considering making a new API with .Net Core, but as far as I can tell from the research I have done, there is no equivalent process in the Entity Framework Core tools. The closest thing I can find is to reverse engineer the entire database with Scaffold-DbContext, and then use migrations for all future database changes. We can't scaffold the entire database, because some of the tables have errors, and fixing all those errors is not a viable option for us right now.
I have found that I can supply a list of tables that I want scaffolded with the initial Scaffold-DbContext call, but I'm not sure if migrations can be used in a similar way to the Update Model From Database... tool. Can I use migrations to add tables that already exist in our database? If not, what other options should I be looking at?

Extra table in database Entity Framework Code First With Database Migrations

Is it okay to have a table in the database that is not one of the entities when using code first with database migrations? Or will this interfere with the migrations? I want to put in a table to track some miscellaneous information.
We do this with no issues. We do add the table through the migration though so we we can ensure all developers have the same schema. If you want to skip this and go via SQL Management studio then you should have no issued either.
Sql(#"CREATE TABLE.....")

Entity Framework does not read FK Relationships, LINQ To SQL Does

I am trying to create an Entity Model from a database. When I add the tables EF does not read my existing FK constraints. If I make a Linq to SQL file in the same project and add the same tables it reads the constraints without issue.
Whats the deal?
Recently I had the same issue - I am not sure why, but somehow the Entity Framework conflicts with some non-clustered indexes.
Similar situation:
http://blog.degree.no/2012/09/missing-relations-in-entityframework-model-when-generating-from-database/
You can try removing your indexes, regenerate your model, add the indexes again and update the regenerated model - it worked for me.

Making changes to existing entities in Entity Framework

Suppose I have an existing database set up using Entity Framework. Is there a mechanism through which I can safely add or remove entities (or their properties) such that the database is altered automatically?
I know there's an option to "Update Model From Database". Is there an equivalent "Update Database From Model" ? Is there a way to configure Visual Studio to do this automatically?
Entity Framework 4.3 has Code First Migration support.
EF helps you with checking the differences between your code and database and then generates code for you that handles this changes. You can use the NuGet package manager console to enable migrations, add a new migration and run them against your database (or create a sql script).
This blog explains how the Migrations work and this blog shows how you can use it with an existing database
Altering the database schema isn't a straightforward operation (has a column been renamed, or is it new column? Can the old type be converted to the new type?) that you can easily infer from the model.
EF doesn't alter the tables for you - it can Drop-Create the DB for you when you change it. However, if you change the existing database by hand to suit the model, EF doesn't seem to mind. It looks like what they check for is Hash(Model) = Hash(Tables).

Categories

Resources