0

Can anyone help me please? Right now i'm connecting my web API project to an existing database. Someone suggested to use EntityFramework for that. I'm applying code first approach in the "Entity Data Model Wizard", but the problem is i'm actually supposed to just import tables, but in the "Choose your database Objects and Settings" part, i accidentally imported views as well. so i was thinking to undo the import.

I read that after the Code First wizard, an app.config file should be added to the project, but this file is missing. The .edmx file is also not found. I've searched all files in folders but still not found. But when i looked at the ConnectionString tag in the web.config file, the new connection that i created in the wizard is there.

Nurul
  • 147
  • 1
  • 3
  • 15

2 Answers2

0

Please follow below steps to update the EDMX from database again:

Open the EDMX file 2. Right Click on the EDMX file and choose the "Update Model from Database" 3. In the new wizard go to the "Delete" tab and expand the Views 4. Check the Views you want to undo 5. Click on "Finish"

Hope this helps you.

MarsRoverII
  • 111
  • 1
  • 15
  • The EDMX file, is it in the solution explorer? Because i searched for .edmx but no results found. I tried restarting VS but still no results found. – Nurul Oct 27 '16 at 07:22
  • Sorry but what should i look for? Is .edmx the correct extension? – Nurul Oct 27 '16 at 07:47
  • Yes, .edmx is the correct file extension. Try to click on the Show All Files option in the solution. you will get a file with .edmx extension. – MarsRoverII Oct 27 '16 at 09:34
  • Still couldnt find it. When i looked at the ConnectionString tag in the web.config, the new connection that i just created is there, but the .edmx is not found. – Nurul Oct 28 '16 at 00:54
  • I read that after the Code First wizard, an app.config file should be added to the project, but this file is also missing. – Nurul Oct 28 '16 at 01:22
0

First, a couple of remarks:

I'm applying code first approach in the "Entity Data Model Wizard"

Code First means that there is no .edmx file. There are migration files and code mappings.

I read that after the Code First wizard, an app.config file should be added to the project, but this file is missing

Not 100% sure here, but I guess that if your project already has a web.config file it will be used instead of adding a new app.config file (they are basically the same).

So, the thing is that you have to enable and use code migrations, you have to generate POCO classes for your entities (if you don't have them already), and you have to add a database context that extends DbContext and includes DbSets for your entities, and some database initialization code.

This page explains how to do the most difficult part of all of that: dealing with the code migrations. Although it assumes that you are migrating from an existing edmx model and using Power Tools you can just ignore that part and focus on the useful information about the migrations. That is, skip directly to Step 2 in the page.

About removing the views you imported, I guess you didn't get to the part where you generate the migrations, so probably you just have to delete the POCO classes created for the views, and possibly also remove the DbSets added to your DbContext for those entities.

If you generated some migration you can either generate a new migration or modify the existing one. This can be done by adding explicit Ignore mappings for your view entities and running Add-Migration again. If you didn't get to the migrations part yet just ignore this last paragraph.

Hope it helps.

Diana
  • 2,186
  • 1
  • 20
  • 31
  • I've read your link and it seemed like i'm already stuck at step 1. When i right-clicked at my web api project, 'Entity Framework' is not in the list. I'm sorry if i've made a mistake in the explanation, but i'm not migrating from a database first concept. My project is a new project. Its just the database tables and values that are existant. – Nurul Nov 06 '16 at 07:37
  • 1
    You should skip step 1 and go directly to step 2. Or better, as you don't have any previous context file as it is a new project, go directly to step 3. The important thing you have to do is enable migrations and generate your migrations files. – Diana Nov 06 '16 at 11:18
  • I'm assuming here that the wizard still hasn't generated the migrations and DbContext file, is it true? If you already have a Configuration.cs file, a DbContext class, and a Migrations folder containing an Initial migration then you don't need to follow the steps in the doc. – Diana Nov 06 '16 at 11:27
  • I only have the DbContext class file out of the 3 you mentioned. Do i still need to follow the steps in there? – Nurul Nov 07 '16 at 01:20
  • Yes. In order to use Code First you need to enable and generate code migrations. So, please follow steps from 3 on. – Diana Nov 07 '16 at 10:35
  • Error: The project 'myproject' failed to build. – Nurul Nov 08 '16 at 04:35
  • 1
    Make sure your project builds without compilation errors before trying to enable the migrations. – Diana Nov 08 '16 at 19:48
  • Oh okay. I've resolved the build errors and retried the step and now it shows "Migrations have already been enabled in project . To overwrite the existing migrations configuration, use the -Force parameter." I'll get back to you if i have any more problems because at the moment i'm adjusting the database. Or is it alright if i do the foreign key mapping and InverseProperty later on? Thanks so far. – Nurul Nov 09 '16 at 05:09
  • At step 4, the code is incompletely displayed. What class did he implement to the MyDbInitializer class? – Nurul Nov 09 '16 at 08:28
  • 1
    You don't really need that class, you can set directly the database initializer in your DbContext constructor like this: `Database.SetInitializer(new MigrateDatabaseToLatestVersion());` – Diana Nov 09 '16 at 11:30
  • 1
    EF provides several database initializers with different behaviors, you should choose which one you want. `MigrateDatabaseToLatestVersion` is a good candidate. It applies any existing pending migrations to your database at run time so you don't need to run `Update-Database` manually. You still need to generate your migrations with `Add-Migration` every time your model changes. – Diana Nov 09 '16 at 11:35
  • Sorry if its a silly question, but where do i put this: Database.SetInitializer(new MigrateDatabaseToLatestVersion()); ? Is it in the context class(the one that inherits DbContext)? Because i tried so but there's an error: "Declaration expected." I tried changing the Database name to my class's name but also same error. – Nurul Nov 10 '16 at 01:11
  • 1
    In your `DbContext` constructor. Replace `WorkflowDbContext` by your `DbContext`. – Diana Nov 10 '16 at 09:32
  • I've tried but error: "Type argument 'Configuration' does not inherit from or implement the constraint type 'DbContext'. " – Nurul Nov 13 '16 at 01:38
  • Make sure you use the `Configuration` class generated when you did `Add-Migration` the first time. This `Configuration` class must include a type parameter which must be your `DbContext` type. It must be something like this: `internal sealed class Configuration : DbMigrationsConfiguration`. Make sure you only have one `DbContext` and one `Configuration` in your project. – Diana Nov 13 '16 at 11:07
  • Regarding the one you mentioned above, i didn't have to do any change to the auto-generated Configuration class after the Add-Migration step. Because when i checked, it was already correct. It was initially Friend NotInheritable Class Configuration Inherits DbMigrationsConfiguration(Of DbContext). When I tried Step 6: 'Add-Migration initial' without any change, Error: 'The project failed to build.' Then, I tried removed the Friend keyword. But also same error. I also didnt do step 5 because my DbContext is already of DbContext type, not an ObjectContext. – Nurul Nov 14 '16 at 01:06
  • Or does this all happen because while asking you about this migration process, i also did some changes(add foreign key and navigation properties) to some of the model classes that were added after these processes: add item -> ADO.net Entity Data Model -> Code-First from database. Does my editing those model classes be the cause for the failed 'Add-Migration' process? – Nurul Nov 14 '16 at 03:19
  • Sorry Nurul, but this comments thread is getting too long and with too many new questions, I'm losing the track. I think you should create a new question entry with detailed info about your current errors. – Diana Nov 14 '16 at 09:36
  • For everyone's reference, in vb.net, the arrrangement of parameter is the other way round compared to c#. The code that works in vb.net is this one: Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of DbContext1, Migrations.Configuration)) – Nurul Nov 16 '16 at 01:50
  • Guess what, a day after i told you that Add-Migration process didn't work, it did yesterday >.< weird..haha anyways, i've posted a new question to a new problem, could you please have a look at it for me? http://stackoverflow.com/questions/40606167/error-when-update-database-using-code-first-there-is-already-an-object-named – Nurul Nov 16 '16 at 04:20
  • I'm glad it worked! I'll try to have a look at your other question. – Diana Nov 16 '16 at 09:08