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

            }


注意:其他一些针对相同异常的帖子坚持认为这可能是因为缺少主键。但是对于employedetaillogin表,我都有主键和外键。所以不知道出了什么问题。每次将实体对象添加到上下文中时,我都应该调用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: '[email protected]' (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: '[email protected]' (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:具有用于登录的映射的图像
c# - 已成功提交对数据库的更改…ObjectContext可能处于不一致状态-LMLPHP

最佳答案

如果对登录名和EmployeeDetail类使用一对多关系,则在登录类中缺少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。

10-02 23:28