What's the best alternative to DropCreateDatabaseIfModelChanges (MVC3)? - asp.net-mvc-3

I've already checked the other questions and searched in google, but I found a lot of solutions...
What is the best?
I need to Alter the table when the DataModel changes, and I need an automatic Solution (like the DropCreateDatabaseIfModelChanges does..
Thanks a lot for all replies

You can go for CodeFirst Migrations. CodeFirst Migrations. You Need EF 5 binaries.
Run the Enable-Migrations –EnableAutomaticMigrations command in Package Manager Console
Add-Migration will scaffold the next migration based on changes you have made to your model.
Update-Database will apply any pending changes to the database.

To update the Entity Framework EDMX and sync it with your DB changes you double click on the EDMX in the VS2010 Solution Explorer. This brings up the designer (Model Browser window apparently is what it’s called) with a class layout of your entities. Right click anywhere on the designer surface and you’ll see an option in the dialog to “Update Model from Database.” Click on this and you get brought into the Update Wizard where you can choose the database objects you want to Add (if new), but by default it refreshes existing objects.
One thing the wizard doesn’t seem to do is delete columns from existing objects. Probably some sort of safety mechanism, but if you deleted the column from the database, do you really want to keep it in your entity? Ah well, who knows.

It don't works well all the time. In my case I try to create nothing manually with the model editor so each time I modify my database model, I open the EDMX editor, I select all entities and I delete them then, I right click the model and I select option : Update from database.
When the object list appear, I select all table and next. It update the model.

Related

Update Model in Entity Framework

I am trying to make a MVC 3 application that use Entity framework.I use Entity Framework Database First.I made an Sql Server Database with 2 tables ,then i generated with entity framework the model,classes .Then I added an other 2 columns to one of the table.How to update my MVC model with this colums?Exist an automatic way to do this (a command that i can runn to bring all the modifications in my model)?
Thanks
Are you looking at the designer or code view? You can force the designer to open by right clicking on your EDMX file and selecting Open With -> ADO.NET Entity Data Model Designer
Right click on the designer surface of the EDMX designer and click Update Model From Database...
All entities are refreshed by default, new entities are only added if you select them.
Select your xxx.edmx file.
CTRL + A to select all the tables, and then press DELETE to delete them.
right click UpdateModel, select your tabels, views, stored procedures, and include them.
CTRL + S to save and automatically generate the model partial classes

Update Model from Database always shows tables under Add in EF 4

I have an existing MVC3 project using EF4 with a database first design. All entities and the edmx were generated correctly.
The problem is when I add a column to a table or make any change to my database. After making a change, I would go back to VS2010 and open my edmx file. When I right click and select "Update Model from Database" it shows all my tables under "Add" instead of showing only the changes I have made under "refresh". I have searched everywhere to find a reason for this but to no avail. Any help would be great.

Update Model from Database not updating changed data type

I am using Entity Framework 4 with ASP.NET MVC 3.
I have a table, where one of the field called "ABC" was earlier of datatype varchar(100) I have now changed this to nvarchar(256).
Now to update this in Entity Framework, I have done the following steps.
Open .edmx
Right click and selected "Update Model from Database"
Refresh tab
Selected the table I wanted to refresh
Ok
However after doing these steps still in the Property window the "Max Length" shows as 100.
Can some one pls help me on this.
I faced this problem several times.
You can try deleting the ABC property in the edmx and follow the steps you mentioned. It worked for me.
It does not update maxlength of string properties, because the store is not updated.
You can either manually change it in your store, then press update in EF designer. Or you can delete the table, then re-add it and everything will be up to date.(drawback of this approach is that you will lose any custom made changes)
4 Years later and this problem persists. I find the easiest thing to do is to delete the .edmx file and regenerate it again for the cleanest and most dependable update.

Could not find the conceptual model type

I have two Entity Data models within a MVC3 project A and B.
I have recently added the new entity data model B to deal with some new functionality, the issue is that now the existing code has stopped working and I am getting the following error when trying to access code within entity model A.
The error message is:
Could not find the conceptual model type 'project.models.Bclass'
I do not understand why it is this new functionality has affected the current code seeing as entity model A has not change in any way. And functionality B is in a separate class and does not interfere with model A.
When the new code is removed from the project and entity model B is excluded then the code works as it should.
Here is some of the stacktrace from the error:
Exception : Could not find the conceptual model type for 'Project1.Models.CrossSession'.
Application Class - method : System.Data.Metadata.Edm.MetadataWorkspace -- GetEdmSpaceType
User : temp.user
Url : http://localhost/project1/auth/message
Stacktrace : at System.Data.Metadata.Edm.MetadataWorkspace.GetEdmSpaceType(StructuralType objectSpaceType)
at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappings()
at System.Data.Entity.Internal.InternalContext.TryUpdateEntitySetMappingsForType(Type entityType)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
Solution found. It appears in some cases (randomly) there is a mix up in the code generated by the entity framework. This is apparently a known bug and a fix should be coming in future releases. In the mean time the way around this is to move the efm to a separate class within the solution.
One possible explanation is that if you use the database first approach and forget to add the Entity classes, you will get this error.
i.e. (from the guide linked below)
Right click on the model’s designer surface.
From the context menu, choose Add Code Generation Item.
In the Add New Item dialog that opens, select Data from the list of installed templates types on the left.
Choose the ADO.NET DbContext Generator then click the Add
button.
I normally use Julie Lerman's guide; https://msdn.microsoft.com/en-us/library/jj206878(v=vs.113).aspx
Solved this by updating EntityFramework to the latest version (6.0)
http://nuget.org/packages/EntityFramework/.
After installing it regenerate the models and it will work ;)
I had the same problem with 2 different .edmx files in the same project.
For the first .edmx model I had used "Add Code Generation Item", for the second model I hadn't. The project was building correctly but throwing that exception on runtime.
I solved generating the .tt classes for the second .edmx model as well.
Well I just installed EF 4.1 and tried to combine DbContext with a EMDX - When I tried to pass back a view I got the same error - that's why I ended up here... I think the way around this is to separate the functionality out into separate asemblies in the solution...
This can also be resolved by generating the new edmx in visual studio 2010 so it matches the older ones... (And copying over the files it generates)
Using Visual Studio 12, the issue has still not been resolved. As an alternative workaround, I set up the second SQL data model using "old fashioned" Data Classes that pre-date entity models - they're still there and they still work. Use Add - New Item - (Data Category) - LINQ To SQL Classes, open the ServerExplorer, locate the server and database, then drag and drop it onto the data classes pane. Old school - but conveniently overcomes this highly inconvenient issue when the full functionality of entity models is not needed.
I removed the edmx models, removed the Entity package, re-install the entity 5.0 and works.
I was working with some legacy software and was not keen on upgrading EF. For me, the below setting on my newly added .edmx needed to be changed to match the .edmx that already existed in the project. My new one said "T4" and I changed it to "Legacy Object Context". I did have to delete the .tt templates that were generated previously in order to get it to build. Otherwise, the old generated files stay there and the new files are also there, causing "property already defined" errors. Thx to all the answers above that helped me figure this out!
(Properties panel, after clicking in the entity designer)

How do I stop the linq designer overwriting my manual changes?

I like Linq but find that once the designer has created my classes I have to modify them.
Then when I change my database and recreate my classes in the designer my changes get wiped.
For instance, let's say I have a class called Person.
I create this class, add some non database related methods to it (outside of Linq) and then create a database table with a similar name.
Linq will duplicate this class in the designer.cs file when I drag it in.
So I go and add the partial keyword to the class in that file or even change the class name Linq created.
As development proceeds, no doubt the database table changes and I have to redrag my tables into the designer.
Voila, my manual changes get wiped.
This is most annoying!
In fact, its unworkable to keep modifying code I have already modified.
Any suggestions?
Create a partial class for your context and make your changes there, the designer generated .cs files are recreated every time you click save.

Resources