无法将简单的行插入到DB2 w / EF5中 [英] Can't insert simple row into DB2 w/ EF5

查看:321
本文介绍了无法将简单的行插入到DB2 w / EF5中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用EF5和DB2 9.1 for z / OS和IBM数据提供者。我的程序中的一切工作正常,除了这一部分。我不能在数据库中插入一个新对象。我收到错误:


{ERROR [23502] [IBM] [DB2] SQL0407N将NULL值赋值为
NOT NULL column \EMPL_ID\不允许。}


我已经验证了一段时间后,值是NOT null ...它是一个有效的整数。发生了什么?

  if(String.IsNullOrEmpty(emplID))
return null;
try
{
int _emplID = Convert.ToInt32(emplID.Trim());

使用(var ctx = new Data.TIMSContext())
{
var user =(from ct in ctx.Query< Data.Entities.ASNUser>()
其中u.EmployeeID == _emplID
选择u).FirstOrDefault();
if(user == null)
{
//将用户添加到数据库
user = new Data.Entities.ASNUser()
{
EmployeeID = _emplID,
FirstName = firstName.Trim(),
LastName = lastName.Trim()
};
ctx.Set< Data.Entities.ASNUser>()。Add(user);
ctx.SaveChanges();
}

返回新Models.UserInfo()
{
EmployeeID = user.EmployeeID,
DisplayName = String.Format({0} {1},user.LastName,user.FirstName)
};
}
}
catch(异常e)
{
throw;
}


解决方案

问题是我将用户设置为新对象,然后将其添加到DBSet。我猜,创建了用户对象的两个不同的副本,一个空值和一个填充。所以我评论了添加行,它工作正常。


I'm using EF5 and DB2 9.1 for z/OS and the IBM data provider. Everything in my program works fine except for this one part. I can't insert a new object into the database. I get the error:

{"ERROR [23502] [IBM][DB2] SQL0407N Assignment of a NULL value to a NOT NULL column \"EMPL_ID\" is not allowed."}

I have verified time after time that the value is NOT null... it's a valid integer. What's going on?

if (String.IsNullOrEmpty(emplID))
            return null;
        try
        {
            int _emplID = Convert.ToInt32(emplID.Trim());

            using (var ctx = new Data.TIMSContext())
            {
                var user = (from u in ctx.Query<Data.Entities.ASNUser>()
                            where u.EmployeeID == _emplID
                            select u).FirstOrDefault();
                if (user == null)
                {
                    //add user to database
                    user = new Data.Entities.ASNUser()
                    {
                        EmployeeID = _emplID,
                        FirstName = firstName.Trim(),
                        LastName = lastName.Trim()
                    };
                    ctx.Set<Data.Entities.ASNUser>().Add(user);
                    ctx.SaveChanges();
                }

                return new Models.UserInfo()
                {
                    EmployeeID = user.EmployeeID,
                    DisplayName = String.Format("{0}, {1}", user.LastName, user.FirstName)
                };
            }
        }
        catch (Exception e)
        {
            throw;
        }

解决方案

The problem was that I was with setting the user to a new object and then adding that to the DBSet. I guess that created two distinct copies of the user object, one null and one filled out. So I commented out the add line and it worked fine.

这篇关于无法将简单的行插入到DB2 w / EF5中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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