错误 3002:映射片段时出现问题 |时间:2019-03-17 标签:c#linqtoentities

发布于 2024-10-04 12:07:04 字数 1206 浏览 6 评论 0原文

我有一个控制台应用程序,我想做的是每次应用程序运行时,日期和时间都会发送到我的数据库中的表中。

表结构如下:

FTPRuns

ID int

Last Run datetime

足够简单。

我已经更新了我的应用程序中的 model.edmx 也以反映这一新的更改,但是现在我收到以下错误,并且我不完全确定它的含义。

错误 3002:映射问题 从行开始的片段 1330:潜在的运行时违规 表 FTPRuns 的键 (FTPRuns.ID): 列 (FTPRuns.ID) 映射到 EntitySet FTPRuns 的属性 (FTPRuns.ID) 概念方面 但它们不形成 EntitySet 关键属性(FTPRuns.ID、 FTPRuns.LastRun)。

这是我用来更新数据库的代码片段:

 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());
                }
            }

如果有人可以帮助我,我将非常感激:)

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

千寻… 2024-10-11 12:07:04

您的实体模型将 FTPRuns.IDFTPRuns.LastRun 两个属性的组合作为实体键,而您的表只有 FTPRuns.ID 列> 作为主键。因此,在您的模型中,您指定 FTPRuns.ID 和 FTPRuns.LastRun 的组合必须是唯一的,而您的数据库对 FTPRuns.ID 有更严格的要求code> 单独必须是唯一的。

只需从实体键中排除属性 FTPRuns.LastRun 即可。也许这是意外发生的,或者实体框架无法从数据库获取主键信息,而不得不推断实体键。例如,视图没有主键,实体框架会将实体键推断为所有不可为空列的组合。

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.

等风也等你 2024-10-11 12:07:04

Daniel Brückner 解决方案非常适合我!下面基本上是他的指示,但是以图形形式 - 这可能会帮助懒惰的读者:)。

您要注意的是您在模型中的 PK,即

在此处输入图像描述

我们可以看到我有一个名为 id 的 PK。现在,如果我查看我的 EF 模型:

在此处输入图像描述

我只能看到指定的 1 个键,这是正确的。对我来说,情况并非如此,所有 4 列都是键。

如果右键单击该列(在 VS 上的 EF 图表中),您将获得勾选/取消勾选实体键的选项:

在此处输入图像描述

确保这与您的型号匹配。就我而言,只应勾选 id,保存并构建项目。

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.

征棹 2024-10-11 12:07:04

当我更改表(数据库中)中的关键字段并更新实体模型时,我就发生了这种情况。

旧密钥仍然存在于模型中,因此我进入 .edmx 文件中的对象属性并将密钥设置为 False。这解决了它。

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.

凶凌 2024-10-11 12:07:04

我从 edmx 中删除了该表(在 edmx 上选择导致问题的表 -> 右键单击​​ -> 删除)
然后执行“从数据库更新模型”,

为我解决了这个问题

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

绝情姑娘 2024-10-11 12:07:04

我从模型浏览器中删除了实体和类,并从数据库进行了更新,确保选择了表。这为我解决了这个问题。

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.

忱杏 2024-10-11 12:07:04

我在创建新表时忘记设置主键,因此我进入 SQL Management Studio 来设置主键。完成后,我更新了 model.edmx 文件以反映更改并收到 3002 错误。

在更新模型时,它所做的是将表的所有列设置为“实体键”。因此在查看model.edmx文件时,找到相关的表,并右键单击不同的属性,以确保只有主键选择了“Entity key”。这解决了我的问题。

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.

何止钟意 2024-10-11 12:07:04

检查表的主键,如果存在则
1)打开.edmx文件,选择所有表并从模型中删除。
2)从数据库更新模型并再次添加所有必需的表

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

迷迭香的记忆 2024-10-11 12:07:04

我删除了 edmx 中的所有表,然后“从数据库更新模型”。
还要验证是否拥有数据库的所有者。

I erased all tables from the edmx and then "Update model from database".
Also verify to have an owner of the database.

尘曦 2024-10-11 12:07:04

我有一个非常相似的问题,但我相信 VS2022 中的错误是根本原因。我从数据库更新了模型以将新表添加到我的模型中。但由于某种原因,它修改了模型中的现有表以具有多个键。我从数据库验证应该只有一个键,并将其他值更改为不是键。一旦我这样做了,错误就消失了。

I had a very similar problem, but I believe a bug in VS2022 was the underlying cause. I updated the model from the database to add a new table to my model. But for some reason, it modified an existing table in the model to have more than one key. I verified from the database that there should be only one key, and changed the other values to not be keys. Once I did this, the error went away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文