实体框架ctp5使用数据注释的一对多关系

发布于 2024-10-15 16:15:17 字数 360 浏览 2 评论 0原文

我有两个班级大学和系,假设存在一对多关系,即一所大学有许多系

public class University
{   
    public string UniversityId;
    public string UniversityName;
    public List<Department> Departments;
}

public class Department
{
    public string DepartmentId;
    public string DepartmentName;
}

我想使用实体框架数据注释功能ctp5来映射这种关系 有人可以给我指出任何关于数据注释功能的好教程吗

I have two classes university and department, suppose there is one to many relationship i.e one university has many departments

public class University
{   
    public string UniversityId;
    public string UniversityName;
    public List<Department> Departments;
}

public class Department
{
    public string DepartmentId;
    public string DepartmentName;
}

I want to map this relationship using Entity framework data annotation feature ctp5
and also can someone point me to any good tutorial of data annotation features

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

银河中√捞星星 2024-10-22 16:15:17

请参阅此 http://blogs .msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx
欣快是正确的,你不需要注释。然而,如果你想在对象之间建立多种关系,你可能需要使用 Fluent API。

所以你需要的唯一代码是

public class University
{   
    public string UniversityId { get; set; }
    public string UniversityName { get; set; }
    public List<Department> Departments { get; set; }
}

public class Department
{
    public string DepartmentId { get; set; }
    public string DepartmentName { get; set; }
    public University University{ get; set; }

}

See this http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx
Euphoric is correct you do not need annotations. However if you want to have multiple relationships between objects you may need to use the fluent API.

So the only code you would need is

public class University
{   
    public string UniversityId { get; set; }
    public string UniversityName { get; set; }
    public List<Department> Departments { get; set; }
}

public class Department
{
    public string DepartmentId { get; set; }
    public string DepartmentName { get; set; }
    public University University{ get; set; }

}
许你一世情深 2024-10-22 16:15:17

IMO 不需要注释。如果您的上下文中同时存在这两个类,那么框架本身会识别这种关系并根据需要创建表。

并确保创建从部门到大学的参考。

IMO there is no need for anotation. If you have both classes in your context, then framework itself recognizes this relation and creates tables as required.

And to be sure create reference from Department to University.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文