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 FTPRu开发者_开发知识库ns'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.
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.
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.
We can see I have a PK called id
. Now if I look at my EF model:
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:
Make sure this matches your Model. In my case, only id
should be ticked, save and build project.
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.
I erased the table from the edmx (on edmx choose the table that cause problem -> right click - > delete) and then perform "updated model from database"
that fixed it for me
I deleted the entity and class from the Model Browser and did an Update from Database making sure to select the table. This resolved the issue for me.
I forgot to set a primary key when creating a new table, so I went to SQL Management Studio in order to do it. Once done, I updated the model.edmx file to reflect the changes and got the 3002 error.
What it had done, when updating the model, was to set all the columns of the table as "entity keys". So when viewing the model.edmx file, find the relevant table, and right click on the different properties to ensure that only the primary key has "Entity key" selected. That solved my problem.
Check Primary key for the table, if it exists then 1) Open .edmx file, Select all tables and Delete from Model. 2) Update model from DB and Add all the required tables again
I erased all tables from the edmx and then "Update model from database". Also verify to have an owner of the database.
精彩评论