出现InvalidOperationException调用.NET实体框架的SaveChanges时 [英] InvalidOperationException when calling SaveChanges in .NET Entity framework

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

问题描述

我想学习如何使用实体框架,但我已经打了一个问题,我解决不了。 我在做什么是我走过的,我有电影列表,并插入每一个成一个简单的数据库。

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.

这是在code我用

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.

在对数据库的修改是   成功提交,但一个错误   在更新对象时发生   上下文。 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.

我一直无法找出是什么导致了这个问题。 我的数据库表包含三列
编号INT自动增量
名称的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 =真的建议有。这导致编译错误(错误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.

推荐答案

我找到了解决这个。

发生了什么事是,当我创造了我的表,我忘了添加主键和设置(是身份)属性设置为是。然后,我建立了我的实体模型,并得到这个错误。

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.

没有更多的异常:)

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

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