实体框架-“更新条目时发生错误.有关详细信息,请参见内部异常. [英] Entity Framework - "An error occurred while updating the entries. See the inner exception for details"

查看:121
本文介绍了实体框架-“更新条目时发生错误.有关详细信息,请参见内部异常.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,我刚刚开始学习EF Model First,而Im则停留了一段时间.我遇到了这样的错误:

更新条目时发生错误.有关详细信息,请参见内部异常."

我已经在图表上创建了一个简单的模型,生成了数据库,并使用C#编写了简单的代码,仅在表中添加了一行,但是错误始终显示出来.

我用图/生成的DLL/简单的Main/和错误抛出来发布屏幕截图

更大尺寸的链接: http://i.imgur.com/bKGc4wv.png

解决方案

打开Pluralization On.问题是您的模型对象使用的是单数名称(Pupil)约定,而在数据库中您使用的是复数名称Pupilss.

更新

帖子显示了如何打开或关闭它. 该帖子的一些相关摘录:

打开和关闭复数

  • 在工具"菜单上,单击选项".

  • 在选项"对话框中,展开数据库工具". 注意:如果数据库工具"节点不可见,请选择显示所有设置".

  • 单击O/R设计器.

  • 将名称的复数设置为Enabled = False,以设置O/R设计器,使其不更改类名称.

  • 将名称的复数设置为Enabled = True,以将复数规则应用于添加到O/R设计器的对象的类名.

更新2

但是请注意,您应避免使用多个名称. 您可以在此处阅读此处的操作方法(我会在这里引用,以防链接断开).

(...)当您使用Entity Framework Code First方法时,您是从模型类创建数据库表的.通常,实体框架会创建具有多个名称的表.这意味着,如果您有一个名为PhoneNumber的模型类,则Entity框架将为此类创建一个名为"PhoneNumbers"的表.如果您希望避免使用复数名称并且希望使用诸如Customer之类的单数名称,则可以这样做 在您的DBContext类中,重写"OnModelCreating"方法,例如(...)

(...)具有此方法覆盖将避免创建具有复数名称的表.现在它将创建一个名为"PhoneNumber"的表,而不是"PhoneNumbers"的表.(...)

I have problem, I have just started learning EF Model First and Im staying in one point for some time. I got such error :

"An error occurred while updating the entries. See the inner exception for details"

I have created an easy model on diagram, generated the database and wrote easy code in C# to add just one row in the table but the error is showing up all the time.

I post screenshot with Diagram/Generated DLL/Simple Main/And error throwing

Link for bigger size : http://i.imgur.com/bKGc4wv.png

解决方案

Turn the Pluralization On. The problem is that you model object are using singular name (Pupil) convention, while in your database you are using pluralized names Pupils with s.

UPDATE

This post shows how can you turn it on or off. Some relevant excerpt of that post:

To turn pluralization on and off

  • On the Tools menu, click Options.

  • In the Options dialog box, expand Database Tools. Note: Select Show all settings if the Database Tools node is not visible.

  • Click O/R Designer.

  • Set Pluralization of names to Enabled = False to set the O/R Designer so that it does not change class names.

  • Set Pluralization of names to Enabled = True to apply pluralization rules to the class names of objects added to the O/R Designer.

UPDATE 2

But note that, you should avoid pluralized names. You can read here how to do it (I'll cite it here, just in case the link gets broken).

(...) When you work with Entity Framework Code First approach, you are creating your database tables from your model classes. Usually Entity Framework will create tables with Pluralized names. that means if you have a model class called PhoneNumber, Entity framework will create a table for this class called "PhoneNumbers". If you wish to avoid pluralized name and wants singular name like Customer , you can do it like this In your DBContext class, Override the "OnModelCreating" method like this (...)

(...) Having this Method Overriding will avoid creating tables with pluralized names. Now it will create a Table called "PhoneNumber" , Not "PhoneNumbers" (...)

这篇关于实体框架-“更新条目时发生错误.有关详细信息,请参见内部异常.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆