0

我正在尝试为我的 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作为键更好的做法?

4

2 回答 2

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 函数),因此如果这不起作用,您可能需要考虑这一点。

于 2013-09-17T19:52:16.177 回答
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)); 
}
于 2013-09-18T12:56:52.343 回答