Entity framework error on updating model from database and vice versa - entity-framework

Here is what happens when I try to update model from database with VS express 2013 for the web, EF6.1.1 and .NET framework 4.5.
In this case I just added a field to a table in the table definition and updated the database.
After that I right click update model from database in the EDMX model view and I get this error message:
An exception of type ‘System.runtime.interopServices.COMException’
occurred while attempting to update from the database. The exception
message is: ‘A file or folder with the name ‘Model.Context.tt’ already
exists. Please give a unique name to the item you are adding or delete
the existing item first.
I noticed that I get the same error message when I try to generate the database from the model.
I tried the following methods:
How do you update an edmx file with database changes?
http://blog.jongallant.com/2012/08/entity-framework-manual-update.html#.VMYYRv7A7mH
but none of them worked.
I also found that https://entityframework.codeplex.com/workitem/1104 and it seems that it has been resolved with “commit 7e8331d1d22d (EFTools repo)”.
Anyone can help me on how to solve my problem or on what is “commit 7e8331d1d22d (EFTools repo)” ?
Thanks
Boid’

It's an old question but I just run into the same issue. Finally found a solution in this bug report. Check if you have any *.tt files that are not added to solution and remove them. That fixed the problem for me.

Related

using EF migrations with SQL Server (Not express/localdb)

New to EF.
Am using code first but created from an initial database.
First issue is when I added a foreign key to the model After executing add-migrations from the console it created migrations to create the entire tables rather than just adding the foreign keys to the existing tables using AddForeignKey(....),
And when I try to update-database it tries to create a localdb database under and i get the error
Directory lookup for the file "C:\src\Project\App_Data\TheDatabase.mdf" failed with the operating system error 2(The system cannot find the file specified.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Which kind of makes sense because i'm not using sqlexpress.
It seems as though I need some configuration to coax it into action.
Any useful advice would be welcome.
I found a solution although maybe this is more of a workaround
I did change the connection factory but it did nothing. Still tries to use App_Data folder and looks for an MDF there.
I found a work around though. The package manager lets you override the connection string so i just did that and it works fine.
As for the other part of my question regarding why it was trying to create the tables. What I needed to do first it use the Add-Migration with -IgnoreChanges and update-database will then store an initial blank migration so from then on, any changes you make will be just those incremental ones I was referring to.
Here was the link for that. https://msdn.microsoft.com/en-us/data/dn579398.aspx?f=255&MSPPError=-2147217396
Thanks to those who offered help.
Hope this helps. Basically, EF needs your first snapshot of your database to create the model classes as the initial state, after that you can add migrations.
If it doesn't help, can you post your connection string and number of migrations your have?

EF6 Model First: Unable to generate database from model after adding new entities

I created a new project and added an empty data model. I added a few entities and properties and then generated the database according to this tutorial. So far, so good. I then went back and added additional entitities.
Now, I am no longer able to Generate Database From Model... because I receive an Error 11007: Entity Type 'xxxx' is not mapped for all of the new entities I added. According to msdn, I can follow the instructions here to resolve my mapping issue between conceptual and storage models. However, it appears these instructions assume the entities are already present in my storage model (which they are not). When I try to manually map them, the only two tables I have to choose from are the original two tables I created.
I appreciate any help you can offer.
This was a phantom error of sorts. I had another error with a default value I was trying to set for a datetime that appeared at the bottom of my error list. I resolved this error and everything is now working as it should.

Entity Framework - Error 11007: Entity type is not mapped.

I have a problem when I got the lasted .edmx.
I get a message that says
Error 11007: Entity type 'pl_Micro' s not mapped.
Looking at the designer view, I do see that this table does indeed exist.
How can I overcome this meesage?
From MSDN:
Error 11007: Entity Type Is Not Mapped
This error occurs when an entity type in the conceptual model is not mapped to the data source. To resolve this error, map the entity type to the data source. Look at the MSDN link
How to: Map Entities to Database Tables
I get this error when I move a database table to a different schema or delete a database table. In this case Entity Framework doesn't seem to update the edmx file correctly when you Update Model from Database....
The way I resolved this was to do a text search for the name of the offending entity. I then removed all references to this in the xml view of the edmx file, deleted the corresponding cs file that defines the entity, and any other references in the project.
This error was passed to me by the SQL Server Authentication server did not have the necessary permissions and could not see the entities.
Confirm that your sql user has the necessary permissions in the database.
This happens also when you import tables via diagram in edmx and then you point in app.config to different instance of database (switching from dev db to test db for example)
I got this error because I had copy pasted an entity and modified it. Restarting visual studio resolved this.
Correct, close visual study, enter the * .edmx file with a notepad ++, sublime text, notepad, etc., search the table, search the column and modify the column size (MaxLength), in all matches, in My case was 2, but they can be more. Start visual studio and the change is already accepted.
I got this error because I had to update the table column type.
Refresh table in SQL server and re-add table in the entity framework for resolving this.

Entity Model Issue: Not able to create model for one particular table

While creating the entity model I am getting the following error .
Added the connection string to Web.Config.
Successfully registered the assembly 'System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in Web.Config.
ERROR: Unable to generate model because of the following exception: 'Value does not fall within the expected range.'.
Loading metadata from database took 00:00:02.1203632.
Generating model took 00:00:00.8390920.
Writing out the EDMX file took 00:00:00.
All the tables are getting created but the BC_States table is not getting created.I can't understand why.
I've just been investigating a similar issue. It turns out that Entity Framework outputs additional error information in the Messages section of the Error List window in Visual Studio.
As you can see it's much more useful than the generic error that appears in the Output window. This was crucial in helping me to solve my problem.
The question is not readable but I found the similar question at Ado.Net Entity Data Model Not Updating Correctly.
As for me (I had the same problem) the issue was because of there was no primary key set on one of the tables.

Number of members in conceptual type does not match with number of members on object side type

I'm using .net framework 3.5 SP1.
After adding a column to one table in Sql Server (as well as changing an existing column from allowing nulls to not nullable), I can no longer run my project without getting this error:
The number of members in the
conceptual type
'XBRLDimensionalModel.axis_t' does not
match with the number of members on
the object side type
'EOL.Xbrl.Persistence.Data.axis_t'.
Make sure the number of members are
the same.
I gave up trying to find and fix the generated code. I now have deleted all my local entity-related files and re-generated them by starting over from scratch and adding a new item (ADO.NET Entity Data Model). I still get this error.
The only way I can run the project now is to undo all my pending changes and use the last version from source control, and of course change the two modified database columns to nullable.
From all I've read so far it seems like I simply should have been able to "update" my model from the database. That resulted in this exception (above). But now I'm totally confused that even with a complete regeneration of the entity model and supporting classes I'm still getting that error.
I changed the property on my edmx model: "Metadata Artifact Processing" to "Copy to Output Directory". The Designer.cs, csdl, msl, ssdl files all seem to be consistent with the latest DB changes.
The exception is being thrown the first time my entityModel instance is referenced. So it is prior to any loading or saving of the data from the changed table.
any ideas where I'm going wrong?
Thanks,
TG
This error can also happen if the EDMX file was changed outside Visual Studio. Right click on the EDMX file and click "Run Custom Tool"
Open your model as XML. Remove all references to that type from the CSDL. Save and close, then reopen in the GUI. Now you should be able to update model as usual. If that doesn't work, do the same thing, but remove from MSL as well.
This seems a little verbose for a comment so I'm adding this as another answer:
In response Craig's suggestion I opened the edmx file in an XML viewer and removed all references to Axis_t (including the associations due to Foreign Keys). From the entire file.
I then "updated" the model by opening the edmx file as the GUI interface, right-click | refresh from database | Add (tab) which now only lists the Axis_t table. I added the table which seemed to work fine and included my new column and the column was mapped correctly.
I then ran the project to the same result. Same error as posted above.
I have now reverted back to what was in source control as well as changing the database columns (new one and modified one) as nullable. The project runs fine. I still have not successfully been able to implement the new DB column in EF. It behaves as if there is some stored/compiled version of the model which is not being updated via the "update" process.
When changing a foreign key from nullable to non-nullable (or vice-versa), be sure to change the association multiplicity from 0..1 to 1 (or vice-versa). The designer sometimes misses this in an update from the database.
Right click on your edmx file and open with XML.
Find the incorrect data types and change them.
Save file.
This worked for me.
I was facing the same issue, when I added one column to db and added respective property to entity(edmx).
I was also using source control for my solution. As I noticed, while saving changes to any file, save action prompts with overwrite option.
So, I overwritten the designer.cs as well config file and problem gets resolved.
regards,
bhushan
This has happened to me when I am working on two versions of the same application (with Model differences) that compile into the same folders.
It appears that Visual Studio doesn't "clean out" the "obj" temp folder properly and some fragments of the old model are still in there.
If I simply delete all files from the "obj" folder and re-compile this error goes away.
The Model in each version is perfect which was driving me crazy.
Not saying this is the answer to the OP's question but it is definately another reason for this error.
Steve
I had this same issue for hours. Found that in my Designer.cs file one of the properties of my entity was missing its attributes. (Don't know how this happened?)
[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = false)]
[DataMemberAttribute()]
public global::System.Int32 ContractCapacity
Now the error message makes sense, this was the missing DataMember. Once the attribute was added IT WORKED!!!
Here's a nightmare scenario that I just experienced: I have an MVC2 website and a WCF service that are built separately, but share a configuration. Within both projects, I used the same Entity Container Name; therefore I picked up the same connection string for both projects. Eventually the metadata got out of sync between the two and caused this error. The obvious solution was to not use the same Entity Container Name in the two projects; changing to a different name allowed me to specify unique connection strings, and therefore metadata, for each component which avoided the problem.
Now that I figured it out, it's obvious, but I had a tense hour or two!