C# 从子总和记录更新父项
我在visual studio 2012中创建了解决方案,该解决方案在4个项目中:C# 从子总和记录更新父项,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我在visual studio 2012中创建了解决方案,该解决方案在4个项目中: 项目表示层(asp.net mvc) 业务实体层 public class Parent { public int Id {get; set;} public decimal TotalPrice {get; set;} //the rest of properties public private ICollection<Child> Children {get; set;} }
public class Parent
{
public int Id {get; set;}
public decimal TotalPrice {get; set;}
//the rest of properties
public private ICollection<Child> Children {get; set;}
}
public class Child
{
public int Id {get; set;}
public decimal UnitePrice {get; set;}
//the rest of properties
public int ParentId {get; set;}
public private Parent Parent {get; set;}
}
在插入/更新/删除子项时,是否有更好的实现方法按每个子项单价的总和更新父项合计
-我不知道计算总和必须在哪里实现,在存储库中还是在Bll中
如果你能帮助我,我将不胜感激
goodby soon通常,我们避免存储计算属性,但如果:
- 涉及子实体,以及
- 子实体的更新频率低于检索父实体的频率
public class ParentChildBll
{
public decimal UpdateTotal(int parentId, decimal oldUnitPrice, decimal newUnitPrice)
{
var parent = GetParent(parentId);
parent.TotalPrice += newUnitPrice - oldUnitPrice;
UpdateParent(parent);
}
public void AddChild(Child child)
{
var oldUnitPrice = 0m;
var newUnitPrice = child.UnitePrice;
using (var repository = new ChildRepository())
{
repository.AddChild(child);
repository.Save();
}
UpdateTotal(child.ParentId, oldUnitPrice, newUnitPrice);
}
}
通常,我们避免存储计算的属性,但在以下情况下可能有用:
- 涉及子实体,以及
- 子实体的更新频率低于检索父实体的频率
public class ParentChildBll
{
public decimal UpdateTotal(int parentId, decimal oldUnitPrice, decimal newUnitPrice)
{
var parent = GetParent(parentId);
parent.TotalPrice += newUnitPrice - oldUnitPrice;
UpdateParent(parent);
}
public void AddChild(Child child)
{
var oldUnitPrice = 0m;
var newUnitPrice = child.UnitePrice;
using (var repository = new ChildRepository())
{
repository.AddChild(child);
repository.Save();
}
UpdateTotal(child.ParentId, oldUnitPrice, newUnitPrice);
}
}
在EF code first中,您可以使用
计算列
存储总价
。它应该是一个只读
字段
public class Parent
{
public int Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public decimal TotalPrice
{
get
{
return Children == null ? 0 : Children.Sum(x => x.UnitePrice);
}
}
//the rest of properties
public virtual ICollection<Child> Children { get; set; }
}
公共类父类
{
公共int Id{get;set;}
[数据库生成(DatabaseGeneratedOption.Computed)]
公共十进制总价
{
得到
{
返回Children==null?0:Children.Sum(x=>x.UnitePrice);
}
}
//其余的财产
公共虚拟ICollection子项{get;set;}
}
首先,在EF代码中,您可以使用计算列
存储总价
。它应该是一个只读
字段
public class Parent
{
public int Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public decimal TotalPrice
{
get
{
return Children == null ? 0 : Children.Sum(x => x.UnitePrice);
}
}
//the rest of properties
public virtual ICollection<Child> Children { get; set; }
}
公共类父类
{
公共int Id{get;set;}
[数据库生成(DatabaseGeneratedOption.Computed)]
公共十进制总价
{
得到
{
返回Children==null?0:Children.Sum(x=>x.UnitePrice);
}
}
//其余的财产
公共虚拟ICollection子项{get;set;}
}
您只需在linq代码中使用求和函数,即可从数据库中获取父对象
例如,您可以在业务层编写代码
public IEnumerable<Parent>GetAllParents()
{
return context.parents.Select(p=> new Parent
{
Id = p.Id,
TotalPrice = p.Children.Sum(x=>x.UnitePrice)
});
}
public IEnumerableGetAllParents()
{
返回context.parents.Select(p=>newparent
{
Id=p.Id,
TotalPrice=p.Children.Sum(x=>x.UnitePrice)
});
}
我认为不需要更新total方法,您只需在linq代码中使用sum函数即可从数据库获取父对象 例如,您可以在业务层编写代码
public IEnumerable<Parent>GetAllParents()
{
return context.parents.Select(p=> new Parent
{
Id = p.Id,
TotalPrice = p.Children.Sum(x=>x.UnitePrice)
});
}
public IEnumerableGetAllParents()
{
返回context.parents.Select(p=>newparent
{
Id=p.Id,
TotalPrice=p.Children.Sum(x=>x.UnitePrice)
});
}
我认为不需要更新Total Methode您是否在计算后将计算出的父级价格存储在数据库中?为什么?它可以是一个计算值。但它是从子属性计算的,而不是从父属性计算的。计算后是否将计算出的父属性价格存储在数据库中?为什么?它可以是一个计算值。但它是从子属性计算的,而不是从父属性计算的。谢谢您,但您所说的“一般来说,我们避免存储计算的属性,但如果涉及子实体,并且子实体更新的频率比检索父实体的频率低,它可能会很有用”是什么意思?,子实体的更新频率如何低于父实体的检索频率?如果是updateChild,旧单价和新单价会变成什么?分配什么?是的,我是,我想知道什么时候计算属性,但它可能有用,但它不是很少被修改,它修改了每个子积垢。如果是updateChild,旧单价和新单价会变成什么?关于分配了什么?是的,我知道,但在子实体更新的情况下,这些变量分配了什么?谢谢,但“一般来说,我们避免存储计算属性,但如果涉及子实体,并且子实体更新的频率比检索父实体的频率低,这可能会很有用”是什么意思?,子实体的更新频率如何低于父实体的检索频率?如果是updateChild,旧单价和新单价会变成什么?分配什么?是的,我是,我想知道什么时候计算属性,但它可能有用,但它不是很少被修改,它修改了每个子积垢。如果是updateChild,旧单价和新单价会变成什么?分配给什么?是的,我知道,但在子更新的情况下,分配给这些变量的是什么?