如果我想将两个不同表中的 2 个值相乘,我应该在迁移中输入什么? ASP.NET MVC Entity Framework
What should I type in migration if I want to multiply 2 values from two different tables ? ASP.NET MVC Entity Framework
这就是我想完成的 "Calculate total animal maintenance cost. Which is obtained by adding the food cost (food cost * animal weight), housing cost." 我不知道如何计算所有(PetRescued 的 PetWeight Table)*(PetSpecies 的 FoodCost Table)并将其插入我的 MaintenanceCost Table.
和
例如,我有 4 种宠物,猫、狗、兔子和啮齿动物。宠物的 HousingCost 是 10,20,30,40.This 是 Table 1. 对于 Table 2 即 PetRescued,我如何计算 HousingCost 的总和有多少 cats/dogs/.../... 我在我的 PetRescued 数据库中,并将其插入到我的 MaintenanceCost Table.
这是我的 PetRescued 实体,也是 Table
之一
public class PetRescued
{
public int Id { get; set; }
public string PetName { get; set; }
public int PetAge { get; set; }
public string PetGender { get; set; }
public double PetWeight { get; set; }
public DateTime? DateWhenRescued { get; set; }
public string PetBreedName { get; set; }
public PetSpecies PetSpecies { get; set; }
public byte PetSpeciesId { get; set; }
public PetSanctuary PetSanctuary { get; set; }
public byte PetSanctuaryId { get; set; }
}
这是我的 PetSpecies 实体,它包含 FoodCost 和我的第二个 Table
public class PetSpecies
{
public byte Id { get; set; }
public string SpeciesName { get; set; }
public double FoodCost { get; set; }
public double HousingCost { get; set; }
}
这是我的 MaintenanceCost 实体,用于存储计算结果
public class MaintenanceCost
{
public byte Id { get; set; }
public double TotalFoodCost { get; set; }
public double TotalHousingCost { get; set; }
public double TotalVetBillCost { get; set; }
}
您需要的查询是这样的。它插入一个包含所有成本总和的实体。
INSERT INTO MaintenanceCost (TotalFoodCost,TotalHousingCost,TotalVetBillCost)
Values (
(SELECT SUM(PetRescued.PetWeight * PetSpecies.FoodCost FROM PetRescued INNER JOIN PetSpecies ON PetRescued.PetSpeciesId = PetSpecies.Id)),
(SELECT SUM(PetSpecies.HousingCost FROM PetRescued INNER JOIN PetSpecies ON PetRescued.PetSpeciesId = PetSpecies.Id)),
"the amount of TotalVetBillCost"
)
然而,这种方法违反了数据库的基本规则之一,即从不存储可以从数据库中存储的其他数据中检索的数据。另外,很明显,当其他两个表的数据发生变化时,这个实体将不再有效。
这就是我想完成的 "Calculate total animal maintenance cost. Which is obtained by adding the food cost (food cost * animal weight), housing cost." 我不知道如何计算所有(PetRescued 的 PetWeight Table)*(PetSpecies 的 FoodCost Table)并将其插入我的 MaintenanceCost Table.
和
例如,我有 4 种宠物,猫、狗、兔子和啮齿动物。宠物的 HousingCost 是 10,20,30,40.This 是 Table 1. 对于 Table 2 即 PetRescued,我如何计算 HousingCost 的总和有多少 cats/dogs/.../... 我在我的 PetRescued 数据库中,并将其插入到我的 MaintenanceCost Table.
这是我的 PetRescued 实体,也是 Table
之一public class PetRescued
{
public int Id { get; set; }
public string PetName { get; set; }
public int PetAge { get; set; }
public string PetGender { get; set; }
public double PetWeight { get; set; }
public DateTime? DateWhenRescued { get; set; }
public string PetBreedName { get; set; }
public PetSpecies PetSpecies { get; set; }
public byte PetSpeciesId { get; set; }
public PetSanctuary PetSanctuary { get; set; }
public byte PetSanctuaryId { get; set; }
}
这是我的 PetSpecies 实体,它包含 FoodCost 和我的第二个 Table
public class PetSpecies
{
public byte Id { get; set; }
public string SpeciesName { get; set; }
public double FoodCost { get; set; }
public double HousingCost { get; set; }
}
这是我的 MaintenanceCost 实体,用于存储计算结果
public class MaintenanceCost
{
public byte Id { get; set; }
public double TotalFoodCost { get; set; }
public double TotalHousingCost { get; set; }
public double TotalVetBillCost { get; set; }
}
您需要的查询是这样的。它插入一个包含所有成本总和的实体。
INSERT INTO MaintenanceCost (TotalFoodCost,TotalHousingCost,TotalVetBillCost)
Values (
(SELECT SUM(PetRescued.PetWeight * PetSpecies.FoodCost FROM PetRescued INNER JOIN PetSpecies ON PetRescued.PetSpeciesId = PetSpecies.Id)),
(SELECT SUM(PetSpecies.HousingCost FROM PetRescued INNER JOIN PetSpecies ON PetRescued.PetSpeciesId = PetSpecies.Id)),
"the amount of TotalVetBillCost"
)
然而,这种方法违反了数据库的基本规则之一,即从不存储可以从数据库中存储的其他数据中检索的数据。另外,很明显,当其他两个表的数据发生变化时,这个实体将不再有效。