EF Code First ASP.NET C# 设计
我尝试首先使用 EF Code 创建数据库,但我不确定如何定义以下关系的类以使其高效。
我们有一个用户,用户有朋友,它又是用户的集合,所以我在考虑以下 POCO 类,
`//Class User
public class User
{
public Guid UserId{get;set;}
public string UserName{get;set;}
public String UserAddress{get;set;}
// other user properties
}
//Class Friend
public class Friend
{
public Guid FriendId{get;set;} //unique identifier for Friend Table
public virtual User CurrentUser{get;set;}
public List<User> lstUserFriends {get;set;} //contains list of all friends that the user has
}`
这在性能方面看起来不错吗?或者您认为您可以建议替代方案吗?
I am trying to use EF Code first to create a database but I am not sure how to define the classes for the following relationship so that it is efficient.
We have a User and User has Friends, which in turn is a collection of User, so I was thinking of the following POCO Class
`//Class User
public class User
{
public Guid UserId{get;set;}
public string UserName{get;set;}
public String UserAddress{get;set;}
// other user properties
}
//Class Friend
public class Friend
{
public Guid FriendId{get;set;} //unique identifier for Friend Table
public virtual User CurrentUser{get;set;}
public List<User> lstUserFriends {get;set;} //contains list of all friends that the user has
}`
Does this look good performance-wise or do you think you can suggest an alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不直接做
Why not just do
您需要自引用多对多关系,因为用户可以有许多朋友,也可以是许多用户的朋友。
或者您可以省略
InverseProperty
数据注释并使用流畅的映射:You need self referencing many-to-many relation because use can have many friends but also can be friend of many users.
Or you can omit
InverseProperty
data annotation and use fluent mapping: