I am making a web app and as database provider I am using postgresql and Ef Core as ORM. I have two classes:
public class Price
{
public int PriceId { get; set; }
public DateTime TimeStamp { get; set; }
public double Value { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
}
public class Company
{
public Company()
{
Prices = new HashSet<Price>();
}
public int CompanyId { get; set; }
public string Acronym { get; set; }
public string FullName { get; set; }
public virtual ICollection<Price> Prices { get; set; }
}
My issue is that whenever I am trying to add a few records for first company:
var company = Context.Companies.FirstOrDefault(c => c.CompanyId == 1);
company.Prices.Add(new Price {Value = 123.45, TimeStamp = DateTime.Now});
and then for another
var company = Context.Companies.FirstOrDefault(c => c.CompanyId == 2);
company.Prices.Add(new Price {Value = 123.45, TimeStamp = DateTime.Now});
instead of:
CompanyId | PriceId | TimeStamp | Value
-------------------------------------------
1 | 1 | foo | bar
1 | 2 | foo | bar
2 | 1 | foo | bar
2 | 2 | foo | bar
I get:
CompanyId | PriceId | TimeStamp | Value
-------------------------------------------
1 | 1 | foo | bar
1 | 2 | foo | bar
2 | 3 | foo | bar
2 | 4 | foo | bar
My Pgadmin 4 configuration: