Okay, I've spent few days reading looking for the answer, and,finally, found it.
As I sad I'm using EF CF approach.
Generation of InitialSchema is a tricky thing in CF.
I have applied some changes to my POCO classes:
public class Product : Entity
{
public string Description { get; set; }
public bool IsNew { get; set; }
public int Discount { get; set; }
[DefaultValue(true)]
public bool IsActive { get; set; }
[Required]
public string Name { get; set; }
public int CategoryId { get; set; }
[Required]
public virtual Category Category { get; set; }
public virtual ICollection<Photo> Photos { get; set; }
public virtual ICollection<Shop> Shops { get; set; }
public virtual ICollection<Sale> Sales { get; set; }
}
public class Sale : Entity
{
public bool IsActive { get; set; }
public int Price { get; set; }
public string Description { get; set; }
[Required]
public string Name { get; set; }
public int ProductId { get; set; } <-- added FK
public virtual Product Product { get; set; }
public virtual ICollection<Photo> Photos { get; set; }
}
EF CF is smart enough to understand dependencies and build appropriate relations, our goal is just to help CF do it correctly.
- One moment, CF uses naming convention FK = /TableName here/Id
- Does not care how to write Id, ID, id - CF will ignore casing.
- I added explicit FKs to each entity I have, CategoryId, etc.
It's not enough, next point: I have Entity class, which for now is very simple:
public class Entity
{
// no attr here.
public int Id { get; set; }
}
Don't need to add Identity option as attribute, as I did in before(see origin question).
CF uses convention and treats Id as key, by default.
So, I have rebuild my Initial schema using
Enable-Migrations -ProjectName "MyProject"; add-migration InitialSchema -ProjectName "MyProject.Contracts"
then
Update-Database -ProjectName "MyProject.Contracts" -StartUpProjectName "MyProject.Web" -ConnectionStringName "MyProjectContext"
as result, I have correct URLs for one-to-many
/Sales/List.aspx?ProductId=1
My head was blowing about above, but finally it's solved, hope it will help you to save time. Thanks