数据库更改已成功提交... ObjectContext可能处于不一致状态 [英] The changes to the database were committed successfully...The ObjectContext might be in an inconsistent state

查看:674
本文介绍了数据库更改已成功提交... ObjectContext可能处于不一致状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


对数据库的更改已成功提交,但更新对象上下文时发生错误。 ObjectContext可能处于不一致状态。内部异常消息:无法为实体类型Evalv.Services.employedetail

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: Unable to set field/property logins on entity type Evalv.Services.employedetail

登录和雇员的实体框架类设置字段/属性登录

Entity framework classes for login and employee

public partial class login
{
    public string password { get; set; }
    public string status { get; set; }
    public bool active { get; set; }
    public string emailId { get; set; }
    public int employeeId { get; set; }
}

public partial class employedetail
{
    public employedetail()
    {
        this.logins = new HashSet<login>();
    }

    public int employeeId { get; set; }
    public string firstName { get; set; }
    public string middleName { get; set; }
    public string lastName { get; set; }       
    public string emailId { get; set; }               

    public virtual ICollection<login> logins { get; set; }
}

保存数据的方法

//employedetail e  and login l are entity objects with data passed to below method.

public string RegisterUser(employedetail e , login l)
            {
                try
                {
                    using (EvalvEntities context = new EvalvEntities())
                    {
                        context.employedetails.Add(e);
                        context.logins.Add(l);
                        if (context.SaveChanges() > 0)
                        {
                            return "Registered";
                        }
                        else
                        {
                            return "Not Registered";
                        }
                    }
                }
                catch(Exception ex)
                {
                    return "Error :" + ex;
                }

            }

注意:其他一些针对相同异常的帖子坚持认为,这可能是因为缺少主键。但是我有主键的& employedetail login 表的外键。因此不确定发生了什么问题。每次我们将实体对象添加到上下文中时,我是否应该调用 context.SaveChanges()

Note : Some other posts for same exception insisted this could be because of absense of primary keys. But I have primary key's & foreign key for both employedetail and login tables. So not sure what's going wrong. Should I call context.SaveChanges() every time we add entity object to context?

例如

context.employedetails.Add(e);
context.SaveChanges();
context.logins.Add(l);
context.SaveChanges();

还是有更好的方法呢?
我们将不胜感激。

or is there any better way to do this? Any help is greatly appreciated.

更新:在记录为context.savechanges()生成的查询时,得到以下查询&例外

UPDATE : On Logging the query generated for the context.savechanges() i got following query & Exception

INSERT [dbo].[employedetails]([employeeId], [firstName], [middleName], [lastName], [emailId])
VALUES (@0, @1, NULL, @2, @3, )
-- @0: '4567' (Type = Int32)
-- @1: 'sam' (Type = String, Size = 255)
-- @2: 'anderson' (Type = String, Size = 255)
-- @3: 'sam@gmail.com' (Type = String, Size = 255)
-- Executing at 30-12-2016 12:17:27 AM +05:3
-- Completed in 1 ms with result: 1


INSERT [dbo].[login]([employeeId], [emailId], [password], [status], [active])
VALUES (@0, @1, @2, NULL, @3)
-- @0: '4567' (Type = Int32)
-- @1: 'sam@gmail.com' (Type = String, Size = 255)
-- @2: '123456' (Type = String, Size = 255)
-- @3: 'False' (Type = Boolean)
-- Executing at 30-12-2016 12:17:28 AM +05:30
-- Completed in 2 ms with result: 1

引发的异常:EntityFramework.SqlServer.dll中的 System.InvalidOperationException

Exception thrown: 'System.InvalidOperationException' in EntityFramework.SqlServer.dll

更新2 :具有登录名
映射的图像

Update 2 : Image with mappings for login

推荐答案

如果您要使用一对多关系进行登录和EmployeeDetail类,则您将缺少公用虚拟已使用的雇员雇员详细信息{get;设置;} 在登录类中。

If you are using one to many relationship for login and EmployeeDetail class then you are missing public virtual employedetail employeedetail {get; set;} in login class.

请在您的登录类中添加此行。(因此,您会遇到此问题。)

Please add this line in your Login class.(Because of this you are getting this problem.)

public partial class login
{
    public string password { get; set; }
    public string status { get; set; }
    public bool active { get; set; }
    public string emailId { get; set; }
    public int employeeId { get; set; }
    public virtual employedetail employeedetail {get; set;} //Add this in your class
}

注意:-班级名称优先字母应该在大写字母中,并在您的班级名称中更正employeeetail的拼写与EmployeeDetail。

Note: - Class name first letter should be in Capital and correct the spelling of employedetail to EmployeeDetail in your class Name.

这篇关于数据库更改已成功提交... ObjectContext可能处于不一致状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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