我正在尝试为我的Web应用程序正确构建我的数据库。我不确定继续使用密钥和关系的正确方法。基本上..
我的项目包含产品
其中可包含多个报告,但同一报告不与多个产品相关。
我有用户,可能会向他们提供许多报告..无论报告是哪种父产品。
到目前为止我的结构
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", }
);
}
我不确定如何将报告正确分配给<strong>用户,甚至不知道我是否在正确的轨道上。另外,有没有比使用 int 作为键更好的做法?
答案 0 :(得分:1)
虽然我没有使用你上面的方法..这是我的抨击:
protected override void Seed(Insight.Data.InsightDb context)
{
var product1 = new Product{ Name = "Product 1"};
var product2 = new Product{ Name = "Product 2"};
var reports1 = new List<Report>
{
new Report { Name = "Report A", Product = product1, },
new Report { Name = "Report B", Product = product1, },
new Report { Name = "Report C", Product = product1, },
new Report { Name = "Report D", Product = product2, },
new Report { Name = "Report E", Product = product2, }
};
context.Products.AddOrUpdate(p => p.ProductID,
product1,
product2
);
context.Reports.AddOrUpdate(p => p.ReportID,
reports1
);
context.Users.AddOrUpdate(u => u.UserID,
new User { FirstName = "Frank", LastName = "Reynolds", Reports = reports1 },
new User { FirstName = "Dee", LastName = "Reyonolds" },
);
}
我只是不确定设置报告部分是否正确(向AddOrUpdate函数添加一个列表),因此如果这不起作用,您可能需要查看这些内容。
答案 1 :(得分:0)
我找到了解决方案。事实证明,我对多对多关系很感兴趣,报告和用户之间有一个桥接表。这些是我建立这种关系的实体。
public class Report
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual Product Product { get; set; }
public virtual ICollection<User> Users { 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; }
}
然后使用代码优先方法填充我的数据
protected override void Seed(Insight.Data.InsightDb context)
{
Product product1 = new Product { Name = "Product 1" };
Product product2 = new Product { Name = "Product 2" };
Report reportA = new Report { Name = "Report A", Product = product1 };
Report reportB = new Report { Name = "Report B", Product = product1 };
Report reportC = new Report { Name = "Report C", Product = product1 };
Report reportD = new Report { Name = "Report D", Product = product2 };
Report reportE = new Report { Name = "Report E", Product = product2 };
List<User> users = new List<User>();
users.Add(new User { FirstName = "Dee", LastName = "Reynolds", Reports = new List<Report>() { reportA, reportB, reportC } });
users.Add(new User { FirstName = "Frank", LastName = "Reynolds", Reports = new List<Report>() { reportC, reportD, reportE } });
users.ForEach(b => context.Users.Add(b));
}