System.ArgumentException while updating Model from Database - asp.net-mvc

Our Application is an MVC Application. We are using Entity Framework. When I am updating the model to add a table from the database. I am recieving an exception and it says
"An exception of type 'System.ArgumentException' occured while attempting to update from the database. The exception message is: 'An entry with the same key already exists'."
I am not able to figure out what is the problem. Can I have a solution to get over the problem.

I had it just as you did. You probably have two identical nodes: EntitySetMapping. You should remove one and everything will be ok.

I ran into this issue today. It means that you have two definitions of some sort with the same name. In my case, it was a duplicate EntitySetMapping. It happened as a result of me migrating some customizations from an old version of my model into a new version. I copied an EntitySetMapping with custom insert/delete/update mappings, but I didn't think to delete the mapping that had been previously auto-generated by the model designer.
Unfortunately, you won't know you have this problem until the next time you attempt to update from the database, meaning there's potential for this one to go undetected for some time.
In the future, when making significant changes to the model via the XML editor, I would recommend that you do a test database update just to make sure that all is well.

I recently ran into a similar issue with EF6 where this error was occurring and there was no duplicate key anywhere... That were visible in the edmx. What I had to do was right-click in the edmx and select Model Browser. In the Model Browser view and under Model/Entity Types, some lingering entities were there. For some reason, deleting all the entities in the edmx actually didn't do what you would think. Removing these lingering Entities in the Model Browser solved my problem. Hopefully this will solve some people's problems because this type of solution is easy to fix but hard to find.

Probably, there is another table with the same key exists. Can we see the code? Read more on this exception here.

Well, it seems I figured out a fix for my case.
Step 1: I deleted the table from the edmx model that contained the reference or what ever that was causing the error.
Step 2: I right clicked the designer and click "Update Model from Database" again and all was fixed.
FYI, the only way I knew which table to delete was because it was the last one modified since the "Update Model from Database" worked last.
Taken From Here

This is a more detailed answer based on Tazos333's answer.
Finding the actual duplicate is the hardest part. Using Powershell it can be done by:
Get-Content .\models.edmx | Group-Object | Where-Object { $_.Count -gt 1 } | Select -ExpandProperty Name
In order to get as few false positive results as possible, run this only for the <!-- C-S mapping content --> section (copy-pasted in other text file).
Besides scalar properties duplicates (quite normal, since tables might contain the same column names), the duplicate <EntitySetMapping Name="..."> will be very obvious.

Related

what's causing this Entity Framework 6 runtime error - type or namespace does not exist?

This is an ASP.NET (Framework v. 4.5.2) website currently running reliably in production. A new requirement called for adding a new column to an existing SQL Server table (apProduct) which has been done. I then made the necessary changes to the .edmx file, controllers, views, etc., and everything is compiling fine. However I'm now getting the runtime error below:
When I debug and set a breakpoint where apDepartment is first getting created, I expand the created objects and see this:
My attention is drawn to: "The metadata for 'EntityFrameworkDynamicProxies-AnkoMVC' is invalid."
Here is where apProduct is defined, clearly within the AnkoMVC.Models namespace:
Lastly, here is the DbContext class:
Can anyone offer suggestions on how to overcome this?
I found the problem in the #model reference in many of the views. Here is the faulty code:
This is the corrected #model reference:
Thanks!

"In the uniqueness constraints attribute for entity, comma is not valid property" in Coredata Xcode 8

I created a database with Core Data on iOS. I initially set up a unique constraint in my Conversation entity. However, after removing it, I was getting an error: "In the uniqueness constraints attribute for entity Conversation, comma is not a valid property".
I looked at every field in my DataModel.xcdatamodeld but could not find any solution.
If you have set any constrains (using Attribute name) previously for your Entity and later if you would have deleted/renamed that particular attribute, then it will throw such error. You need to rename or delete that constraint property by double clicking it with respect to your data model business flow.
Refer screenshot for reference.
I had the same issue with Xcode 8.1. I did not want to delete all the many attributes painfully set up on all 6 of my entities, and found another solution. Let's assume your model file is called "foobar.xcdatamodeld".
Edit the XML directly in the model. For this, you need to get to the file 'contents' inside the model file. The path to it is: foobar.xcdatamodeld/foobar.xcdatamodel/contents. To get to it in Finder, control-click on the "xcdatamodeld" package to "Show Package Contents", then again on the "xcdatamodel" package. You can then drag the file 'Contents' into your text editor of choice. You will see the constraints appear as follows in the XML:
<uniquenessConstraints>
<uniquenessConstraint>
<constraint value="property_name"/>
</uniquenessConstraint>
</uniquenessConstraints>
You can edit "property_name" to match the new value, or remove the constraints altogether.
Once edited, try to clean and recompile. In my case, the error was still there, so I had to do a little "renaming dance" with Xcode. I deleted the model from the Xcode project, then renamed the model file to a different name (including the "xcdatamodel" file inside the "xcdatamodeld" file), and added it back to Xcode. I repeated that again with the old name again so I was back to the initial name.
Its looks like a bug in Xcode 8. I have deleted all the attributes from the entity, cleaned the project and then added the attributes again. After that, the error was gone. Maybe this will help someone.
I inspected the XML file and noted that the "comma,separated,properties" was in the XML. I used Product->Clean (command+shift+K) to clean the project. Cleaning in of itself didn't help. I then tried File->Save (command+S).
File->save updated the XML and cleared up the situation for me.

Add many attributes to my xdatamodel (iOS Core Data)

I've got a list of 2000+, almost the same, 'NSString' attributes I need to "import" in my .xcdatamodel-file.
If I could open the xcdatamodel-file using an XML-like-text-editor, like a .plist-file, I can add all of my attributes, but I can't find a way how.
I managed to programmaticly create the attributes (using this tutorial), but then I can't set or fetch the attribute's data.
The list should look like:
["str_1_1"],["str_1_2"],["str_1_3"],...,["str_49_4"],["str_49_5"],...
Is there a way to programmaticly add attributes / set and fetch data from attributes?
OR
Is there a way to staticly add all possible attributes without clicking the +button over 2000 times?
You can just open the model file in any text editor. If you have the file compatibility for the file set to Xcode 4 or higher, it's even easy to edit.
The model Foo.xcdatamodel is actually a directory. Inside that is a file named contents, which is nicely formatted, easily readable XML. Edit that. A string attribute will look something like:
<attribute name="stringAttribute" optional="YES" attributeType="String" syncable="YES"/>
Add one or two string attributes in Xcode and then duplicate/edit them as needed.
A couple of notes:
Obviously, it's your job to get the syntax right. This is not documented but also not hard to figure out. If you end up with a broken model file that won't compile, you got something wrong.
It's probably a good idea to quit Xcode first. It might not freak out if you edit the model file while it's running, but you never know.
Having 2000+ string attributes is frankly terrifying and suggests an extremely bad data model. Before editing the model and adding all of these, please carefully consider whether there's a less extreme solution.

XPO Import - Relation is incomplete due to missing fields

I have been setting up builds for quite some time now. To do this, I use the scripts Microsoft provided for AX 2012 (Build and deploy scripts for Microsoft Dynamics AX 2012)
There were some tweaks to be done in the scripts to get TFS working the way it should and it also involved some extra actions because we have code in the startupPost (fe precompilation with message window instead of compiler output form due to modification in the sysSetupFormRun class)
But what is haunting me for some weeks now is the XPO import. The provided script uses the latest CombineXPO tool to combine all of the XPO files that were fetched from TFS into one big XPO. Once that is done, the XPO is imported in Ax.
And the real problem here is that I do not trust the XPO import because we have frequently been seeing huge amounts of errors like :
Compiler ERROR: \Data Dictionary\Tables\EPSICParameters\EPSICParameters : Relation Currency is incomplete due to missing fields
And indeed the fields aren't there in Ax, but when I look in the XPO that was supposed to be imported,the relation fields are present which indicated that the sources were fetched fine from TFS.
REFERENCE #Currency
PROPERTIES
Name #Currency
Table #Currency
RelatedTableCardinality #ZeroOne
Cardinality #ZeroMore
RelationshipType #Association
UseDefaultRoleNames #Yes
ENDPROPERTIES
FIELDREFERENCES
REFERENCETYPE PKFK
PROPERTIES
Field #CurrencyCode
RelatedField #CurrencyCode
SourceEDT #CurrencyCode
ENDPROPERTIES
ENDFIELDREFERENCES
ENDREFERENCE
Anyone who could help me out here? This thing is really blocking our automated builds with Ax because we simply cannot tell when the next build is going to run fine :s
I had this error as well. I believe that the root cause of this is due to the relation being auto created when you drag and drop an EDT onto a table to create a field, and then a rename of that field breaking the table relation. However, the EDT relation will still work on the field and the front end/GUI will not break. For example, dragging the HcmApprover EDT onto a table will prompt you to ask if you want to add the ForeignKey relation from the EDT to the current table? If you say yes, and then rename the field from HcmApprover to something else, the table relation will break. However, the front end will appear to work correctly (you will likely still be able to see a working dropdown to view hired workers from the HCM module).
I'm not positive, but I think the GUI still works because of the EDT relations on the field itself causing the front end to still operate correctly.
Either way, if you drag and drop EDTs (this goes for more than just EDTs) to create fields and do any renaming, make sure that the appropriate auto/framework-generated "stuff" is also renamed manually (ie by you).
Try doing the import twice, ignore any errors from the first run.

TClientDataset: 'Fieldtype not supported for XML.'

I've got a bunch of data loaded into a TClientDataset, representing an array of complex objects. But when I try to run
Dataset.SaveToFile('c:\test.xml', dfXMLUTF8);
it doesn't like it:
Project testing.exe raised exception class EDBClient with message 'Fieldtype not supported for XML.'.
This is a lot less useful than it should be, for two reasons. First off, it doesn't say which field or which field type isn't supported, and second, the actual saving is taking place inside a black-box DLL.
The only field types I'm using in this dataset are integers, strings, booleans, and a few TArrayFields that hold arrays of integer fields. Nothing I'd expect to be all that difficult to serialize. Anyone have any idea why this isn't working?
Is everything saved or just some fields? Maybe for example TArrayFields are throwing an exception? Try removing different fieldtypes one-by-one and see when things start working.
Wild guess is that array fields are not supported in XML export,
but you should check.
Go to Project options->Compiler and turn on "Use debug DCUs". Rebuild.
Set breakpoint on your SaveToFile() call. Run.
Then you can step into VCL source and try to hunt for what is unsupported.

Resources