实体框架 4.1 无效的对象名称
我在实体框架中收到以下错误,但我一生都无法找出导致该错误的原因。
无效的对象名称“dbo.User”
数据库中的实际表名称称为“Users”,我通过右键单击并选择“从数据库更新模型”将其添加到我的实体模型中我的项目中有大约六个其他模型都连接到具有复数表名的同一数据库,并且以完全相同的方式更新,没有任何问题。似乎出于某种原因,EF 在尝试执行更新时从我的表名中删除了“s”。正如您从下面的代码中看到的,我在更新之前执行了一个选择,这没有问题。
我还尝试删除实体并重新创建它,取消选中“复数或单数生成的对象名称”。然而问题仍然存在。
对此有任何想法将不胜感激。
public void Update(User updatedUser)
{
using (var context = CreateUserContext())
{
var entity = context.UserEntities
.Where(u => u.UserId == updatedUser.UserId)
.SingleOrDefault();
if (entity != null)
{
entity.Name = updatedUser.Name;
entity.Description = updatedUser.Description;
entity.LastModifiedById = updatedUser.LastModifiedById;
entity.IsDeleted = updatedUser.IsDeleted;
context.UserEntities.ApplyCurrentValues(entity);
context.SaveChanges();
}
}
}
I am getting the following error in Entity Framework and cannot for the life of me figure out what is causing it.
Invalid object name 'dbo.User’
The actual table name in the database is called Users and I added it to my Entity Model by right clicking and selecting ‘Update Model From Database’ I have around six other models in my project all connecting to the same database which have pluralised table names and are updated in exactly the same way without any problems. It seems for some reason EF removes the ‘s’ from my table name when trying to perform an update. As you can see from my code below I perform a selection before the update which works without problem.
I have also tried deleting the entity and recreating it unchecking the ‘Pluralize or singularize generated object names’. However the problem remains.
Any ideas would be appreciated on this one.
public void Update(User updatedUser)
{
using (var context = CreateUserContext())
{
var entity = context.UserEntities
.Where(u => u.UserId == updatedUser.UserId)
.SingleOrDefault();
if (entity != null)
{
entity.Name = updatedUser.Name;
entity.Description = updatedUser.Description;
entity.LastModifiedById = updatedUser.LastModifiedById;
entity.IsDeleted = updatedUser.IsDeleted;
context.UserEntities.ApplyCurrentValues(entity);
context.SaveChanges();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,您可以选择用户,这让我相信 EF 映射是可以的。
检查表上是否有触发器错误地使用
dbo.User
作为表名称。附带说明一下,您可以执行以下两件事来帮助诊断此类问题:
尝试在 SSMS 中手动更新行。这将告诉您问题是否出在 EF 或 SQL Server(触发器、约束等)
使用 SQL Profiler 检查 EF 发送到数据库的 SQL 语句。
要检查 SQL 语句,您还可以使用 LINQPad,尽管(此时)您需要 使用最新的测试版来使用 EF 4.1。
The fact that you can select users leads me to believe the EF mapping is ok.
Check for a trigger on the table that is incorrectly using
dbo.User
as the table name.As a side note, two things you can do to help diagnose a problem like this:
Try updating the row manually in SSMS. This would tell you if the problem was with EF, or SQL Server (a trigger, constraint, etc.)
Use SQL Profiler to check the SQL statements that EF is sending to the database.
To check the SQL statements, you can also use LINQPad, although (at this time) you will need to use the latest beta to work with EF 4.1.