实体框架更新说,当实体属性为null [英] Entity Framework says property is null when updating entity

查看:137
本文介绍了实体框架更新说,当实体属性为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个新的实体添加到现有的集合。但是当这样做的父实体抱怨其他的导航属性为null(虽然他们不是)



错误:




输入
'System.Data.Entity.Validation.DbEntityValidationException的异常出现在Ela.Facade.dll
,但用户代码没有处理



其他信息:验证失败的一个或多个实体。
见EntityValidationErrors属性的更多详细信息。



基金国家:修改



错误:为必填项所有者




在调试现场业主是否正确装入:
调试价值观



基金类:

 公共类基金
{
公众诠释FundId {搞定;组; }

[必填]
[指数(IDX_FundName,2,isUnique设置= TRUE)]
[最大长度(25)]
公共字符串名称{;组; }

[必填]
[指数(IDX_FundIdentifier,2,isUnique设置= TRUE)]
[最大长度(25)]
公共字符串标识{搞定;组; }

公共双平衡{搞定;组; }

[必填]
[指数(IDX_FundName,1,isUnique设置= TRUE)]
[指数(IDX_FundIdentifier,1,isUnique设置= TRUE)]
公共虚拟ApplicationUser所有者获得{;组; }

公共虚拟的ICollection<&交易GT;交易{搞定;组; }

公共基金()
{
事务=新的List<&交易GT;();
}
}



CreateTransaction方式:

 公共事务CreateTransaction(交易newTransaction)
{
VAR背景=新ApplicationDbContext();

{
VAR基金= context.Funds.FirstOrDefault(F => f.FundId == newTransaction.ToFund.FundId);
newTransaction.ToFund =基金;
fund.Transactions.Add(newTransaction);
context.SaveChanges();
}
赶上(DbEntityValidationException E)
{
的foreach(在e.EntityValidationErrors VAR前夕)
{
Console.WriteLine(式\\的实体\\{0} \状态\{1} \具有以下验证错误:
eve.Entry.Entity.GetType()名称,eve.Entry.State);
的foreach(VAR已经在eve.ValidationErrors)
{
Console.WriteLine( - 物业:\{0} \错误:\{1} \ ,
ve.PropertyName,ve.ErrorMessage);
}
}
扔;
}
返回context.Transactions.FirstOrDefault();
}



任何帮助或建议表示赞赏!


< DIV CLASS =h2_lin>解决方案

在查找基金,它不填充外键的属性,如果他们是虚拟的。为了让他们拉你必须包括该财产;这样做将让你得到想要的结果。

  VAR基金= 
context.Funds
。包括(F => f.Owner)
.FirstOrDefault(F => f.FundId == newTransaction.ToFund.FundId);

您可以找到有关负载如何EF相关实体在此附加信息的 MSDN文章


I'm trying to add a new entity to an existing collection. But when doing so the 'parent' entity complains the other navigational properties are null (although they aren't).

Error:

An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in Ela.Facade.dll but was not handled in user code

Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Fund State: Modified

Error: Field Owner is required

When debugging the field Owner is loaded correctly:

Fund class:

public class Fund
{
    public int FundId { get; set; }

    [Required]
    [Index("IDX_FundName", 2, IsUnique = true)]
    [MaxLength(25)]
    public string Name { get; set; }

    [Required]
    [Index("IDX_FundIdentifier", 2, IsUnique = true)]
    [MaxLength(25)]
    public string Identifier { get; set; }

    public double Balance { get; set; }

    [Required]
    [Index("IDX_FundName", 1, IsUnique = true)]
    [Index("IDX_FundIdentifier", 1, IsUnique = true)]
    public virtual ApplicationUser Owner { get; set; }

    public virtual ICollection<Transaction> Transactions { get; set; }

    public Fund()
    {
        Transactions = new List<Transaction>();
    }
}

CreateTransaction method:

public Transaction CreateTransaction(Transaction newTransaction)
{
    var context = new ApplicationDbContext();
    try
    {
        var fund = context.Funds.FirstOrDefault(f => f.FundId == newTransaction.ToFund.FundId);
        newTransaction.ToFund = fund;
        fund.Transactions.Add(newTransaction);
        context.SaveChanges();
    }
    catch (DbEntityValidationException e)
    {
        foreach (var eve in e.EntityValidationErrors)
        {
            Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                eve.Entry.Entity.GetType().Name, eve.Entry.State);
            foreach (var ve in eve.ValidationErrors)
            {
                Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                    ve.PropertyName, ve.ErrorMessage);
            }
        }
        throw;
    }
    return context.Transactions.FirstOrDefault();
}

Any help or recommendations are appreciated!

解决方案

When you look up the fund, it does not populate the foreign key properties if they are virtual. In order to have them pulled you have to include that property; doing so will allow you to get the desired results.

var fund = 
    context.Funds
           .Include(f => f.Owner)
           .FirstOrDefault(f => f.FundId == newTransaction.ToFund.FundId);

You can find additional information about how EF loads related entities in this MSDN article

这篇关于实体框架更新说,当实体属性为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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