Error 3002: Problem in mapping fragments | c# linq to entities

I have a console application and what I'm trying to do is that every time the appllication runs, the date and time is sent to a table within my database.

The table structure is like so:

FTPRuns

ID int

Last Run datetime

Simple enough.

I've updated the model.edmx within my application also to reflect this new change, However now I am getting the below error and I'm not entirely sure what it means.

Error 3002: Problem in mapping fragments starting at line 1330:Potential runtime violation of table FTPRuns's keys (FTPRuns.ID): Columns (FTPRuns.ID) are mapped to EntitySet FTPRuns's properties (FTPRuns.ID) on the conceptual side but they do not form the EntitySet's key properties (FTPRuns.ID, FTPRuns.LastRun).

Here is the snippet of code that I use to update the database also:

 using (ModelContainer ctn = new ModelContainer())
            {
                try
                {
                    FTPRun ftp = new FTPRun
                    {
                        LastRun = DateTime.Now
                    };

                    ctn.FTPRuns.AddObject(ftp);

                    int changes = ctn.SaveChanges();

                    Console.WriteLine(changes.ToString() + " Changes saved");
                    Console.WriteLine("The LastRun Date Has Been Updated");
                }
                catch (InvalidOperationException ex)
                {
                     Console.WriteLine(ex.ToString());
                }
            }

If anyone can help me I'd be very grateful :)

thanks.


Solution 1:

Your entity model has the combination of the two properties FTPRuns.ID and FTPRuns.LastRun as entity key while your table has only the column FTPRuns.ID as primary key. So in your model you specify that the combination of FTPRuns.ID and FTPRuns.LastRun must be unique while your database has the stronger requirement that FTPRuns.ID alone must be unique.

Just exclude the property FTPRuns.LastRun from the entity key. Maybe this happened accidentally or the Entity Framework could not get primary key information from the database and had to infer the entity key. For example views have no primary key and the Entity Framework will infer the entity key as the combination of all non-nullable columns.

Solution 2:

Daniel Brückner solution worked perfectly for me! Below is essentially what he instructed, but in a graphical form - which may help the lazy readers :).

What you want to look out for is that your PK in the Model i.e.

enter image description here

We can see I have a PK called id. Now if I look at my EF model:

enter image description here

I can see only 1 key specified, which is correct. For me, this was not the case, all 4 columns were keys.

If you right-click the column (in EF diagram on VS) you will get the option to tick/untick the Entity Key:

enter image description here

Make sure this matches your Model. In my case, only id should be ticked, save and build project.

Solution 3:

this happened to me when I changed the key field in the table (in the database) and updated the entity model.

The old key was still there in the model, so I went into the object's properties in the .edmx file and set the key to False. That fixed it.