在.NET Entity框架中调用SaveChanges时,InvalidOperationException异常 [英] InvalidOperationException when calling SaveChanges in .NET Entity framework

查看:92
本文介绍了在.NET Entity框架中调用SaveChanges时,InvalidOperationException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用Entity框架,但是我遇到了一个我无法解决的问题。
我正在做的是,我正在浏览我有的电影列表,并将每个电影插入一个简单的数据库。

I'm trying to learn how to use the Entity framework but I've hit an issue I can't solve. What I'm doing is that I'm walking through a list of Movies that I have and inserts each one into a simple database.

这是代码我正在使用

private void AddMovies(DirectoryInfo dir)
{
    MovieEntities db = new MovieEntities();
    foreach (DirectoryInfo d in dir.GetDirectories())
    {
        Movie m = new Movie { Name = d.Name, Path = dir.FullName };
        db.AddToMovies(movie);
    }
    db.SaveChanges();
}

当我这样做时,我在db.SaveChanges()读取一个异常

When I do this I get an exception at db.SaveChanges() that read.


数据库的更改是
提交成功,但更新对象$ b时发生错误
$ b上下文。 ObjectContext可能在
中是不一致的状态。内部异常
消息:AcceptChanges不能继续
,因为对象的键值

ObjectStateManager中的另一个对象冲突。在调用
AcceptChanges之前,确保
键值是唯一的。

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

我还没有找出导致这个问题的原因。
我的数据库表包含三列

Id int autoincrement

名称nchar(255)

路径nchar(255)

I haven't been able to find out what's causing this issue. My database table contains three columns
Id int autoincrement
Name nchar(255)
Path nchar(255)

更新:
我检查了我的edmx文件和SSDL部分有建议的StoreGeneratedPattern =身份。我也遵循博客文章,并尝试在CSDL中添加ClientAutoGenerated =true和StoreGenerated =true。这导致编译错误(错误5:不允许ClientAutoGenerated属性。)。因为博客帖子是2006年,它有一个链接到后续的帖子,我认为它已经改变。

Update: I Checked my edmx file and the SSDL section have the StoreGeneratedPattern="Identity" as suggested. I also followed the blog post and tried to add ClientAutoGenerated="true" and StoreGenerated="true" in the CSDL as suggested there. This resulted in compile errors ( Error 5: The 'ClientAutoGenerated' attribute is not allowed.). Since the blog post is from 2006 and it has a link to a follow up post I assume it's been changed.

但是,我看不到后续的帖子,因为它似乎需要一个msdn帐户。

However, I cannot read the followup post since it seems to require an msdn account.

推荐答案

我找到了解决方案。

发生了什么事,当我创建表时,我忘记添加主键并将(Is Identity)属性设置为yes。我然后创建了我的实体模型并得到这个错误。

What happened was that when I created my table I forgot to add the primary key and set (Is Identity) property to yes. I then created my Entity model and got this error.

我回去修复了我的数据库表,但我仍然隐藏了奇怪的异常。最终解决的问题是删除实体并在表被修复后重新创建。

I went back and fixed my database table but I still hade the weird Exception. What solved the problem in the end was to remove the entity and re-create it after the table was fixed.

没有更多的例外:)

这篇关于在.NET Entity框架中调用SaveChanges时,InvalidOperationException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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