Cryptic Linq to Entities message - c#

I get the following message:
{"Entities in 'Entities.ApprovalRequests' participate in the 'FK_ApprovalRequest_Audit' relationship. 0 related 'Audit' were found. 1 'Audit' is expected."}
I'm stumped, does anyone know what to make of it?
My EDMX had the FK and is correct, yet every time I get this message.
How one would go to debug this, would be most useful.

You probably tried to persist an entity of type ApprovalRequests with no associated entity of type Audit while your entity model specifies that each ApprovalRequests must have (at least) one Audit.

Related

System.Data.Entity.Core.EntityCommandExecutionException Occurred in MVC app Using EF

I am about to create an Application Form with UUID as its unique key (not primary key).
I got the error:
An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: An error occurred while executing the command definition. See the inner exception for details.
Connection String To DB:
connectionString="Data Source=(localdb)\v11.0;
Initial Catalog=FormsContext;
Integrated Security=True;
MultipleActiveResultSets=True;
AttachDbFilename=|DataDirectory|Forms.mdf"
providerName="System.Data.SqlClient"
FormsContext.cs:
namespace ApplicationForm.Models
{
public class FormsContext : DbContext
{
public FormsContext() : base("name=FormsContext")
{
}
public System.Data.Entity.DbSet<ApplicationForm.Models.Form> Forms { get; set; }
...
The database is like this:
FormsContext->Forms->{id,uuid,first,last...}
I am not so sure what happened this time. Would anyone help me to go through this?
Edit/Resolution
I wasn't sure how to check Inner Exception and I did some research on this. So when I read the error message in the Inner Exception, I could solve my problem.
I found out what the bug was. The query field is 'firstname' but my table column name is 'first'. They did not match. Once I changed the DB column name, it was working again.
found out what the bug was. The query field is 'firstname' but my table column name is 'first'. They did not match. Once I changed the DB column name, it was working again.
One must be diligent to keep each in sync if the database is always in flux.
Why?
EF is simply a set of mappings, a snapshot in time, and whether those mappings are to tables or stored procedures, it does not matter.
For any subtle change(s) to column names or foreign key constraints can have ripple effects to any old mappings and they need to be remapped/scaffolded into new structure(s); otherwise hence weird behaviors from old mappings.
At times, those effects can create obvious errors like you found or even more insidious non obvious logical errors which can provide working (compiled) software with subtle logic bugs. I have seen the logic bugs by experience and they do happen.
So always be wary to keep the EF mappings in sync.
I answered to a different EF question but in a similar vein.
I had run into a situation where in EF a mapped stored proc at runtime failed because an internal change in the stored proc saw that a sql cast had stripped out a column name from the result, and EF (mapped before the changed) failed. I have a screen shot in my answer which shows that EF/db changes can have a deleterious effect:
The data reader is incompatible . A member does not have a corresponding column in the data reader with the same name
In my case helped next:
1) press View Detail.. button:
2) Check your actual error:
Hope it helps for someone.
Just in case this helps someone - I saw this error message. It turned out that I had changed the database associated with my EF model and hadn't set the connection string up properly.

Error 112 and Error 113 after adding foreign keys to database

This is my table ( part of the whole thing , activity table still have other relationships with other tables) :
After adding foreign key and update my model EDMX from database in my visual studio , it came up this 2 error :
Error 2 Error 112: The types of all properties in the Dependent Role
of a referential constraint must be the same as the corresponding
property types in the Principal Role. The type of property
'ActivityID' on entity 'istellarModel1.singalong' does not match the
type of property 'ActivityID' on entity 'istellarModel1.activity' in
the referential constraint
'FK_singalong_activity'.
and
Error 1 Error 113: Multiplicity is not valid in Role 'activity' in
relationship 'FK_singalong_activity'. Because all the properties in
the Dependent Role are nullable, multiplicity of the Principal Role
must be '0..1'.
I checked my ActivityID in singalong have the same type as activity table's Activity ID , and i don't understand what error 113 actually means , i am new to database , at first i have many tables that don't link so i link them up after sometime and update my model ( EDMX ) in visual studio ( using entity framework ) and it gave me errors .
Any guidance on this please?
The 113 error sounds like your activity ID in singalong is NULLable.
It's stating that many-to-one is not valid if the dependent role is nullable.
That also seems to suggest the reason for the 112 - it's most likely complaining because it considers NOT NULL part of the type as well, so the two columns are different.
A quick fix may be to ensure that singalong.ActivityID is marked a NOT NULL but this will be problematic if you want singalong records with no corresponding activity.
I know how I'd fix it, at least initially, but it may be frowned upon be more knowledgeable DBAs: I'd simply add a dummy activity (eg, activity id = 0) to use for those cases where you would normally have NULL in the singalong table. I'm not suggesting you do that, but it's a possibility I would examine as a temporary fix, being far more of a pragmatist than dogmatist :-)
I know this is a old question, but run into the same issue, and here is the fix.
If you open your .edmx file (using entity framework), you will see your different tables. If you click on the line linking the tables:
It will show the properties. Select Multiplicity and set to 0..1
Your foreign key will need to be set as nullable.

Polymorphic cross-associations on Entity Framework

OK, this is an interesting and most importably real urgent problem for me to solve... In order for others to neatly comprehend it, I've stretched myself to make a well illustrated post.
The Object Model
So I have this simple, easy and "beautiful" model in mind. See the first picture. (You can ignore PathEntry, it's not relevant in my situation.)
The idea is that a MediaFeedItem owns:
a collection of ThumbnailFileEntries (accesible through the ThumbnailFiles property)
at most 1 raw FileEntry (MetadataFile property) and
at most 1 MediaFileEntry (MediaFile property)
We shall refer to these last three entity types as the file entities.
Now there's more: As you can see, I am inheriting both ThumbnailFileEntry and MediaFileEntry from FileEntry, and let's not debate that! (for now), it's one of those end-of-story aspects of the design and both entity types will continue to grow later on.
This already brings me some significant issues right away in regards to the polymorphic associations induced by the relationships from the file entities to MediaFeedItem.
The first thing that you shall observe is that I have eliminated the navigation property from the derived file entities (ThumbnailFileEntry and MediaFileEntry) to the primary entity MediaFeedItem.
I do this because they already inherit that property defined in the base class FileEntry. As you can see, I do not delete the roles at the end of these associations.
The Relational Model
I shall be using the so-vastly-conceptually-superior TPT strategy for generating and mapping my Object Model to the RDB world (vs TPH/TPC).
I'm using EF5-rc, the EDMX model designer to design my model, and the EF5 DbContext Generator to generate a DbContext and POCOs cuz I wanna use the DbContext API.
As you can see, I can nicely generate the database model using the EF tools:
The Problem
When loading a new MediaFeedItem and saving it, I get the following error:
System.InvalidOperationException: Multicplicity constraint violated. The role 'MetadataFile' of the relationship 'MediaFeedModel.MediaFeedItem_MetadataFile' has multiplicity 1 or 0..1.
What am I doing wrong?
Looking at your problem one thing stands out, The FK relationship between File and MediaFeedItem is required (IE a file must have a MediaFeedItem), but in the case where you are in an extended version of File you probably dont want this.
What i think you want to do is one of the following:
change the multiplicity on MediaFeedItem_FileEntry to 0..1 - 0..1 so that it isnt required at either end
create a new extended type to handle your metadataFile type and remove the direct reference between the base type and MediaFeedItem
I personally think the second is a more elegant solution to your problem as its creating an actual type for your MetadataFile
What appears to be happening is that you are trying to create an extended type but the base type isnt actually a metadata file.

Entity Framework database-first with SQL Server

I'll try to explain my problem although to be honest I can't even understand it. After many changes in a couple of tables in my DB now I try to create a Foreign key and I'm getting this error when updating the EDMX.
gHOP.msl(410,10) : error 3007: Problem in Mapping Fragments starting at lines 410,
1511: Non-Primary-Key column(s) [UserGUID] are being mapped in both fragments to different conceptual side properties - data
inconsistency is possible because the corresponding conceptual side
properties can be independently modified.
gHOP.msl(1511,6) : error 3012: Problem in Mapping Fragments starting at lines 410, 1511: Data loss is possible in Itinerary.UserGUID.
An Entity with Key (PK) will not round-trip when:
(PK does NOT play Role 'Itinerary' in AssociationSet 'FK_Itinerary_Users' AND PK is in 'Itinerary' EntitySet)
gHOP.msl(410,10) : error 3012: Problem in Mapping Fragments starting at lines 410, 1511: Data loss is possible in Itinerary.UserGUID.
An Entity with Key (PK) will not round-trip when:
(PK is in 'Itinerary' EntitySet AND PK does NOT play Role 'Itinerary' in AssociationSet 'FK_Itinerary_Users' AND Entity.UserGUID
is not NULL)
Honestly, it's been a nightmare because I can't understand what's going on. Although I've given up and I won't create the FK if someone could at least give me a hint I would really appreciate it.
Thanks
Not sure how much access/control you have over the model but I've encountered similar issues before when updating entities in the db and then attempting to update the model. I usually just delete the entities from the model which you edited in the db! Rebuild without them. Then re-add them after the build. I find that Visual Studio is not always successful updating the model when structural/relational changes have been made in the db.

Need help using entities for ASP.NET MVC 2 framework

I'm making a site in ASP.NET MVC 2 using C#. I designed a database with a bunch of tables that have many to many relationships, similar to the following:
GrandParent - many to many - Parent
and
Parent - many to many - Child
I used the Entity Framework to make all of the entities class and am now working on a function in a repository class which adds a GrandParent.
It tries to create Child, add it to a Parent, then add the parent to a GrandParent and then add the GrandParent to the database by using the command
entities.GrandParents.AddObject(newGrandParent);
entities.SaveChanges();
It crashes on the SaveChanges() line with the error:
Unable to update the EntitySet
'JunctionPartentsChilds' because it
has a DefiningQuery and no
element exists in the
ModificationFunctionMapping element to
support the current operation.
EDIT:
I can fix that error by deleting all of the DefiningQuery elements in the auto generated code and now I'm getting an error on the same line that should be more descriptive but I'm still at a loss.
The error is: Invalid object name 'JunctionPartentsChilds' and it throws a UpdatingException.
Any ideas what's going wrong? Do you have to add to the database in a special order because of the many to many relationships?
Thanks!!
If you copy/pasted this error:
Invalid object name 'JunctionPartentsChilds'
Maybe the problem is the misspelling of "Parents" in the junction table name
Your description looks like to one to many relationsships. But anyway, please post your code fragment here, to see if there's an error in your statements.
Bye Thomas
P.S. Ther's a wonderful way to format your code like this
This is a line of code
if you klick the button with the 101
make sure that GrandParents table as a primarykey(identity) and rebuild the model

Categories

Resources