繁体   English   中英

已成功提交对数据库的更改…ObjectContext可能处于不一致状态

[英]The changes to the database were committed successfully…The ObjectContext might be in an inconsistent state

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

登录和员工的实体框架类

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()

例如

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

还是有更好的方法来做到这一点? 任何帮助是极大的赞赏。

更新 :在记录为context.savechanges()生成的查询时,我得到了以下查询和异常

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”

更新2 :具有用于登录的映射的图像 在此处输入图片说明

如果您对登录名和EmployeeDetail类使用一对多关系,那么您将缺少public virtual employedetail employeedetail {get; set;} public virtual employedetail employeedetail {get; set;}

请将此行添加到您的Login类中。(因此,您遇到了这个问题。)

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
}

注意:-类名的首字母应在大写字母中,并在您的类名中将employeeetaile的拼写更正为EmployeeDetail。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM