我正在尝试为我的 Web 应用程序适当地构建我的数据库。我不确定处理键和关系的正确方法。基本上..
我的项目包含一个产品
其中可以包含多个Reports,但是,同一个 Report不能与多个 Products 相关联。
我有Users,他们可能有许多可用的报告。无论报告父产品如何。
到目前为止我的结构
public class Product
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class Report
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual Product Product { get; set; }
}
public class User
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Report> Reports { get; set; }
}
并Seed
protected override void Seed(Insight.Data.InsightDb context)
{
context.Products.AddOrUpdate(p => p.ProductID,
new Product { ProductName = "Product 1" },
new Product { ProductName = "Product 2" }
);
context.Reports.AddOrUpdate(p => p.ReportID,
new Report { ReportName = "Report A", ProductID = 1, },
new Report { ReportName = "Report B", ProductID = 1, },
new Report { ReportName = "Report C", ProductID = 1, },
new Report { ReportName = "Report D", ProductID = 2, },
new Report { ReportName = "Report E", ProductID = 2, }
);
context.Users.AddOrUpdate(u => u.UserID,
new User { FirstName = "Frank", LastName = "Reynolds", },
new User { FirstName = "Dee", LastName = "Reyonolds", }
);
}
我不确定如何正确地将报告分配给用户,甚至不知道我是否走在正确的轨道上。另外,有没有比使用int作为键更好的做法?