【问题标题】:ASP Entity Framework - Code FIrst - Data Design - KeysASP 实体框架 - 代码优先 - 数据设计 - 键
【发布时间】:2013-09-22 08:37:25
【问题描述】:

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

【问题讨论】:

  • 查看我刚刚对您的模型所做的编辑。也许这会有所帮助。如果我的理解是正确的..您想要一个拥有与其相关联的报告列表的用户,并且您希望每个报告中都有一个独特的产品?
  • 谢谢。一旦我在 SQL Server 中并手动填充和查询表,实体模型就会正确排列。您能否提供一个示例以在 Seed() 内的 C# 代码中适当地填充它们?

标签: c# asp.net entity-framework database-design ef-code-first


【解决方案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 函数),所以如果这不起作用,您可能需要查看。

【讨论】:

  • 有趣。我看到这里发生了什么。例如,我尝试将“report1”也添加到 Dee Reynolds(因此他们将拥有相同的报告)并收到错误:集合已修改;枚举操作可能无法执行。我也拿出了 Reoirts.AddOrUpdate...
  • 你能做任何类似 context.Users.AddOrUpdate( ........ ).With( // 这是你要添加报告的地方) 吗?有点像 - stackoverflow.com/questions/11263542/… ,但同样,只是不确定语法。
  • 如果有帮助的话,您正在寻找一对多映射。
  • 谢谢。现在回答就够了。我必须根据我的特定需求对其进行改进
  • 如果您有任何其他具体问题,请私信我。抱歉,我无法为您提供完全的帮助。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-09
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 2015-02-24
    • 2021-01-05
    相关资源
    最近更新 更多