What should I type in migration if I want to multiply 2 values from two different tables ? ASP.NET MVC Entity Framework - sql

This is what am I suppose to complete "Calculate total animal maintenance cost. Which is obtained by adding the food cost (food cost * animal weight), housing cost." I don't know how to do calculation for all (PetWeight from PetRescued Table)*(FoodCost from PetSpecies Table) and Insert it into my MaintenanceCost Table.
And
For example, I have 4 type of pet species which is Cat,Dog,Rabbit and Rodent. The HousingCost for the pets are 10,20,30,40.This is Table 1. For Table 2 which is the PetRescued, How do I calculate the sum of HousingCost for how many cats/dogs/.../... that I have in my PetRescued database and Insert it into my MaintenanceCost Table.
This is my PetRescued entity which is also one of the 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; }
}
This is my PetSpecies entity which consist of the FoodCost and also my second Table
public class PetSpecies
{
public byte Id { get; set; }
public string SpeciesName { get; set; }
public double FoodCost { get; set; }
public double HousingCost { get; set; }
}
This is my MaintenanceCost entity which is use to store the result from the calculation
public class MaintenanceCost
{
public byte Id { get; set; }
public double TotalFoodCost { get; set; }
public double TotalHousingCost { get; set; }
public double TotalVetBillCost { get; set; }
}

The query you need is something like this. It inserts one entity containing sum of all costs.
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"
)
However this approach is against one the basic rules of databases that is never store a data that can be retrieved from other data stored in the database. Also, It is clear that when the data in other two tables alter, this entity will no longer be valid.

Related

How to establish one-to-many relationship for a code-first approach?

I'm trying to build a recipe app for my spouse. I'm trying to set it up so she can add new recipes to the database as the app grows.
When adding new recipe, she will have three drop-down to pick from to construct her new recipe ingredients. First one will contain a list of ingredients that she can choose from, the second one a list of measuring units and the third one a list of quantities.
Here is what I got so far. Am I heading in the right direction or am I off? I'm using Entity Framework with a code-first approach:
public class Recipes
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Image { get; set; }
}
public class Units model
{
[Key]
public int Id { get; set; }
public string UnitName { get; set; }
}
public class UnitQty
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class IngredientsModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class RecipeIngredients
{
public int Id { get; set; }
public int RecipesId { get; set; }
[ForeignKey("RecipesId")]
public Recipes Recipes { get; set; }
public int IngredientsModelId { get; set; }
[ForeignKey("IngredientsModelId")]
public IngredientsModel IngredientsModel { get; set; }
public int UnitQtyId { get; set; }
[ForeignKey("UnitQtyId")]
public UnitQty UnitQty { get; set; }
public int UnitsModelId { get; set; }
[ForeignKey("UnitsModelId")]
public UnitsModel UnitsModel { get; set; }
}
After creating the table, controller and the views, this is what I get in the recipe ingredients index view.
Any suggestion will be more than welcome please and thank you
RecipeIngredient class's view
First of all. You are over engineering your domain model. On relational databases Join is bottleneck you should prevent from joins if it doesn't helps you.
public class Recipt
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public ICollection<RecipeIngredient> Ingredients { get; set; }
}
public class IngredientModel
{
public int Id { get; set; }
public string Name { get; set; }
public IngredientUnit UnitType { get; set; } // Unit model is best to be added here. if it doesn't change in a single IngredientModel.
}
public class RecipeIngredient
{
public int Id { get; set; }
public int UnitQuantiy { get; set; } // No need to more classes.
public IngredientModel Model { get; set; }
public Recipt Recipt { get; set; }
}
public Enum IngredientUnitType // Same Unit Model but less database relation as its small finite collection.
{
Killogram,
Count,
....
}
and according to the Microsoft documents its best to use fluentApi configuration for the relations.
Override this method in your Context:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Recipt>.HasMany(P => P.Ingredients).WithOne(P => P.Recipt);
builder.Entity<RecipeIngredient>.HasOne(P => P.Model);
// There is no need to explicit foreign key definition. but you can explicitly define your foreign keys.
}
And for the last part. in Views you can use extra models called ViewModels.
As above domain turned to a minimal domain you just need to pass a list of IngredientModels to your view to complete your View.

Should I inject another context into SaveChangesInterceptor?

I want to perform auditing, by logging entity changes. I have an Audit class:
public class Audit
{
public int Id { get; set; }
public string UserId { get; set; }
public string Type { get; set; }
public string TableName { get; set; }
public DateTime DateTime { get; set; }
public string OldValues { get; set; }
public string NewValues { get; set; }
public string AffectedColumns { get; set; }
public string PrimaryKey { get; set; }
}
I've created AuditingInterceptor which I will add to multiple contexts. The Audits table is not accessible through these contexts.
internal class AuditingInterceptor : SaveChangesInterceptor
{
public override ValueTask<InterceptionResult<int>> SavingChangesAsync...
}
In order to save to the Audits table should I inject AuditContext that has access to Audits table or should I use another aproach?

Doubts about EF Core 2.1 Relations

I am working on Entity Framework Core Code First approach and ASP.Net Core 2.1 making 3 tables:
Person class
public class Person
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public PeopleProfessions PeopleProfessions { get; set; }
}
Professions' class
public class Profession
{
public string Id { get; set; }
public string Name{ get; set; }
public PeopleProfessions PeopleProfessions { get; set; }
}
peopleprofessions' class
public class peopleprofessions
{
[ForeignKey("PersonId ")]
public string PersonId { get; set; }
public ICollection<Person> People { get; set; }
[ForeignKey("ProfessionId")]
public string ProfessionId{ get; set; }
public ICollection<Profession> Professions { get; set; }
}
On my Context:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<peopleprofessions>().HasKey(up => new { up.PersonId, up.ProfessionId });
}
Bearing this in mind:
People can have multiple professions.
The professions table is only for reading stored data like "Accountant".
I have doubts about how I can make table 3 only contain the foreigners and that it can meet the needs that I just mentioned.
I have tried to make the relationship appropriately but I also noticed that in tables 1 and 2 it requests both Id of the table people's professions.
I don't know if I am lost or if I am looking wrong or if there is an alternative to that situation. Thanks for any help you can give me.
You have the use of Collections on the navigation items a bit backwards. For your primary entities (Person and Profession), they should have collections, since it's one-to-many. But for the PeopleProfessions, each record is a single link to a specific entity, so no collection there just a direct object reference.
public class Person
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<PeopleProfessions> PeopleProfessions { get; set; }
}
public class Profession
{
public string Id { get; set; }
public string Name{ get; set; }
public ICollection<PeopleProfessions> PeopleProfessions { get; set; }
}
public class PeopleProfessions
{
public string PersonId { get; set; }
public Person Person { get; set; }
public string ProfessionId { get; set; }
public Profession Profession { get; set; }
}
You can, but don't need to specify a ForeignKey attribute because you are following EFs naming conventions(it will figure it out for you). Your OnModelCreating looks correct for the composite key.
You may want to consider removing the plural from PeopleProfessions (just call the class PeopleProfession) since one instance represents a single People-Profession relationship. I typically do this and but the navigation name in the entities remains plural, since it can represent more than one, i.e.
public ICollection<PeopleProfession> PeopleProfessions { get; set; }

Automapper generates left join in SQL

I'm using Automapper in my .NET Core 2.2 back-end service. I use ProjectTo method to generate my result.
This is my Product class:
public class Product
{
public DateTime? SellEndDate { get; set; }
public string Name { get; set; }
public bool AllowToShow { get; set; }
public Category Category { get; set; }
public Guid CategoryId { get; set; }
public Brand Brand { get; set; }
public Guid BrandId { get; set; }
}
And it is the result class:
public class Dto
{
public string Name { get; set; }
public CategoryDto Category { get; set;}
public BrandDto Brand { get; set; }
}
The configuration in the profile is a normal one. The generated SQL statement for category table has Inner Join, but for brand table it uses a Left Join.
Why is that?

How to model this correctly in Entity Framework?

I have a requirement that I am not sure how to accomplish, in my existing data I have a list of customers, each customer should be assigned a staffMember to work with them, so would this be a 1 to 1 relationship or a 1 to many relationship, having trouble wrapping my head around how to model the data, as I want to figure out how to model this correctly. Since a staff member can be assigned to many different customers How should I model this? Does this look correct?
What I would like is to have the form pull the list of staff members available from the staff table, when inputting a new customer, ideally by the name
which I figure I could probably do using linq..
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string BusinessName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public DateTime RequestDate { get; set; }
public Staff Staff { get; set; }
public List<CustomerJob> CustomerJobs { get; set; }
}
public class Staff
{
public int ID { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string EMail { get; set; }
public int CustomerId { get; set; }
}
Customer have exactly 1 Staff while a single Staff maybe assigned to more than 1 Customer. So this is a one-to-many relation.
It is better that Customer be aware of its Staff. It could be called AssignedStaff. Staff itslef does not need to have a property to show all its Csutomers. Tough you can extract Customer list of a Staff using a simple query.
My recommended class structure is as follow:
public class Customer
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string BusinessName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public DateTime RequestDate { get; set; }
public Staff AssignedStaff { get; set; }
public List<CustomerJob> CustomerJobs { get; set; }
}
public class Staff
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string EMail { get; set; }
}
A query for extracting Customer list of a Staff:
var customers = _dbContext.Customers.Where(x => x.AssignedStaff.Id == staffId);