Search code examples
c#linq-to-entitiesedmx

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

  • 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.