Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架:如何更新可空属性_Entity Framework_Nullable_Database Update - Fatal编程技术网

Entity framework 实体框架:如何更新可空属性

Entity framework 实体框架:如何更新可空属性,entity-framework,nullable,database-update,Entity Framework,Nullable,Database Update,假设我有这门课: public class BuzzKill { public string Name {get;set;} public DateTime? StrikeDate {get;set;} } 如果我将该类的一个实例写入一个具有空删除项的数据库,然后尝试使用Julia Lehrman的书DbContext中的以下方法对其进行更新和保存,我会收到以下异常: System.InvalidOperationException : The original value for the p

假设我有这门课:

public class BuzzKill
{
public string Name {get;set;}
public DateTime? StrikeDate {get;set;}
}
如果我将该类的一个实例写入一个具有空删除项的数据库,然后尝试使用Julia Lehrman的书DbContext中的以下方法对其进行更新和保存,我会收到以下异常:

System.InvalidOperationException : The original value for the property 'StrikeDate' cannot be set to null because the 'StrikeDate' member on the entity type 'BuzzKill' is not nullable.
显然,该成员可以为null。有人有解决方法或解决方案吗?我用的是EF5.0

下面是我用来更新实体的代码,以及一条注释,说明了引发异常的位置:

 public virtual void ApplyChanges<TEntity>(TEntity root)
            where TEntity : class, IObjectWithState
        {
            using (var context = new AmazingChartsContext())
            {
                context.Set<TEntity>().Add(root);

                CheckForEntitiesWithoutStateInterface(context);

                foreach (var entry in context.ChangeTracker.Entries<IObjectWithState>())
                {
                    IObjectWithState stateInfo = entry.Entity;
                    entry.State = ConvertState(stateInfo.State);
                    if (stateInfo.State == State.Unchanged)
                    {                           
                        ApplyPropertyChanges(entry.OriginalValues, stateInfo.OriginalValues);
                    }
                }

                try
                {
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                   //stuff...

                }
            }
        }

        protected void ApplyPropertyChanges(DbPropertyValues values, Dictionary<string, object> originalValues)
        {
            foreach (var originalValue in originalValues)
            {
                if (originalValue.Value is Dictionary<string, object>)
                {
                    ApplyPropertyChanges((DbPropertyValues)values[originalValue.Key],
                        (Dictionary<string, object>)originalValue.Value);
                }
                else
                {
                    values[originalValue.Key] = originalValue.Value; //here's where the exception gets thrown
                }
            }
        }

我读过关于这个问题的文章,但除了4.3中的一个bug之外,似乎没有人有答案。

您使用的是.NET 4.0还是.NET 4.5?如果.NET4.0,您很可能仍然会遇到此错误,因为Rowan Miller在该线程中说,此错误存在于.NET4.0上的EF5仍在使用的EF4.0核心库中。对于.NET 4.5,您不应该有问题,因为以下线程表明该错误已在.NET 4.5中修复:谢谢,我昨天发布后不久就发现了这一问题,现在正在考虑转换为4.5。我建立了一个玩具项目来测试它,它看起来好像我的问题消失了4.5。我今天要确认我们的实际代码库。感谢您的回复。我刚刚意识到这不是一个选项,因为我们的很多客户仍然运行XP,而XP不支持4.5。哦,好吧,回到绘图板上。。。