ASP 实体框架 - 代码 - 数据设计 - 键

本文关键字:数据 代码 实体 框架 ASP | 更新日期: 2023-09-27 18:31:45

我正在尝试为我的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", }
        );
    }

不确定如何将报告正确分配给用户,甚至不知道我是否在正确的轨道上。另外,有没有比使用 int 作为键更好的做法?

ASP 实体框架 - 代码 - 数据设计 - 键

虽然我没有使用你上面的方法......这是我的刺痛:

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 功能),因此如果这不起作用,您可能需要查看这一点。

我找到了解决方案。事实证明,我对多对多关系感兴趣,在报表和用户之间有一个桥接表。这些是我建立这种关系的实体。

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)); 
}