VS2010 MVC and Entity Framework Model in Separate Project - asp.net-mvc

I am trying to use an Entity Framework Model (in separate project) into an asp.net 4 MVC project (VS2010, C#)
If I create the EF inside the MVC project I have no problems.
I think I am missing some step.
things done:
1. added reference to the EF class project
2. added connection string in MVC web.config
3. added reference to System.Data.Entity in both web.config and project references
Now i can use the model only if I copy the .edmx file to the Models folder, but in this way the EF project is not external anymore.
What am I missing?
Thank you in advance.

Have you set the entity model to create public classes? (Set in the models properties, and each of its entities.)

You seem to done all necessary steps already. Try to remove the reference and add it again and rebuild it.
I had same problem but after rebuild the solution (1 MVC project and 1 data access layer project), everything works fine.

Related

No option to add EDMX file to project in MVC solution

I've created an MVC application. I would like to now add a new class-library project to the solution, and then add an EDMX file to that project.
This used to be easy. But now I have to choose between .NET Core and .NET Standard. But it appears it doesn't matter which I choose because if I create a class library with either of these frameworks, Visual Studio provides no option for adding an EDMX file to them! And I don't see an option to add a standard .NET Frameworks class library.
So what is the secret incantation to add a new project to my MVC solution, and then add an EDMX file to that project?
Note: I do have the option of adding an EDMX file directly to the MVC project. But I would prefer to place it in a separate project, which I've done before.
EDMX file is deprecated with EF Core and I believe it has been removed from .net core projects as well.
If you need to do that, you may need to add it to a normal .net 4.5 project and copy the files but this will make no sense using it with .net core or .net standard.
You should be using Code First anyway as it is cleaner and easier to use.
If you already have the database, you can refer to this link, it can help you to scaffold the classes from the database
https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db
#Jonathan: The EDMX file is old way of doing things. EDMX is nothing but a T4 template which facilitates in creation of edmx.
With .Net Core the best way is to:
1. Create a .net core library project. Add entityframeworkcore nuget package from here
2. The use this link, to generate your model's from DB
The concept of EDMX is not supported anymore.
Hope this helps.
Based on your description you're using the old ASP.NET MVC framework, and not the new Core one, and in this case you're using the Full .NET framework. So, when you create the new Class Library project, you should do it with the Full .NET Framework, because .NET Core and .NET Standard does not support, EDMX. If you chose this one, you can add your ADO.NET Entity Data model to it:
At least for EF v6, if your using EF Migrations, each ('state'/'target') snapshot of the database (stored within the migrations-history db table & migration file) is an EDMX file just waiting to be decoded, saved to a file, and then loaded in VS (Visual Studio). ;)
It's a little in-depth (or at least challenging), but 'System.Data.Entity.Migrations.Edm.ModelCompressor' may present the clue.
Basically, you can extract the EDMX from one/the relevant migration. But I do agree that the Code-First approach should be used for the implementation, the EDMX is only a development-aid these days.

Does Entity Framework need to be installed for all projects in a solution?

I have a VS2013 solution that contains two projects. The first project is my Entity Framework Model and the second project contains the Windows Form I want to use the EF models in.
Does the Entity Framework need to be installed in the Windows Form project if it is making use of the objects from the Entity Framework Model project? Is it enough just to reference the Entity Framework project from within the Windows Form project?
I am using Entity Framework 6 in Visual Studio 2013.
You have to reference the Entity Framework because you are using your models that need the entity framework to work...
Anyway you can try to remove the Entity Framework reference from your Windows Form project and see if it works... If it compile then everything is fine, otherwise you will have to add the reference again.

Entity Framework MVC Controller

I am using Visual Studio 2012. I created a Class project and added EF data model to this project. I created the data model from an existing SQL Server Database. I created a MVC 4 project, added entity connection string to web config, and added data project reference.
All is good to this point.
Now I want to add a MVC controller using Entity Framework. I select a one of the model classes. I select the data context class. Click Add.
VS pops an error up, the last part of the error message is class might be in a compiled assembly {which is true}.
'AA' is not part of the specified 'aanamespace.aaEntity' class and the entity class could not be modified to a DbSet property to it. For example the 'aaEntity' might be in a compiled assembly
I was in the same situation and I created a class library and in it i have all my .edmx and when i have a reference to my mvc project i got the exact the same error message i have posted my question here (stackoverflow.com/questions/18552864/…)
How did I fix:
Close the Visual Studio and open it again...
I want to take the time to answer this question myself so that others with a similar problem can be helped in the future.
Here is what I did.
Created the .edmx file in my Models folder of my MVC project.
Built the MVC project
Now I can add a controller for any of the tables in the .edmx file.
Note:
I thought the best practice was to create a project within my solution that holds the .edmx file. I called this project myData.
I added a reference to this project in my MVC project and tried this scaffolding without luck.
I hope this helps others because the solution is so simple.
Gerry
The problem was caused by the VSCommands for Visual Studio 2012 (Early Access) extension. Once I uninstalled it the problem disappeared.
Run Visual studio (Run as administrator) works for me.
Without seeing the exact error, I can only suggest a problem I experienced with the EF controller scaffolding and how I got around it. If you have extended your EF context class with a partial class, you will have to remove the latter from your project and recompile before generating the controller, otherwise EF scaffolding refuses to work.
Although this is a very old question, but there maybe people like me still facing the same issue which none of the suggested solutions can help them.
The reason for this error message when creating a new controller/scaffolding is the version of your EntityFramework.
HOW TO FIX:
if your project is MVC4 then you should use entity version 5.
if you use Entity version 6 you will face this issue.
remove your .edmx file and add your EntityFramework version 5.
it will work as expected.

How to move models to separate project .NET MVC

I created .NET MVC application and I want to move my models (and then Controllers) to a different project in my solution.
I'm a noob in MVC so I need simple explanation please.
Thank you very much
Add a project to your solution called "YourProject.Models".
Add references to System.Data, System.Data.Linq, System.Data.DataSetExtensions (if you're using Linq to Sql)
Add a reference from your original MVC project to the new "models"project.
Move your model classes from your MVC project to the new project.
Compile and run.
Create a new project with appropriate name and move the models to a new project. It is as simnple as that. Then add a reference to the project you are using. Next step is to Create interfaces and implement those interfaces for code separation. Next use IoC (Inversion of control or dependency injection). I suggest asp.net mvc structure map for Ioc. You can use NuGet tool to do the job for you.
Move your models wherever you want and then update the strongly typed views to match ViewPage<Namespace.For.Your.ModelClass> or maybe change the web.config namespace if you do it that way.

Where do Subsonic classes go in an ASP.NET MVC Project?

I've built ASP.NET webform projects in the past, and when generating Subsonic classes, the teams I have been on have put our Business Layer/DAL objects into a Project.Framework project.
Would that still be a recommended structure, or should the Subsonic classes go directly into the /Model folder within the MVC web project?
It shouldn't go in the Model folder (I think the Model folder should just be used for a Class that is made only for the view and wont be used in the rest of the app).
It should go into a separate assembly Maybe Project.Core or Project.Data
I would not put the Subsonic classes directly in the MVC project. Since you didn't with ASP.NET, there is no reason to change now.
I wouldn't even leave the Controllers in the MVC web project.
You can put them in Model, and move them to their own project later, but its not really any more work to just put them in their own project now, so I would just do that.

Resources