EF Code First ASP.NET C# 设计

发布于 2024-11-05 09:12:34 字数 591 浏览 0 评论 0原文

我尝试首先使用 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 技术交流群。

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

发布评论

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

评论(2

忱杏 2024-11-12 09:12:34

为什么不直接做

public class User
{
  public Guid UserId{get;set;}
  public string UserName{get;set;}
  public String UserAddress{get;set;}
  public IList<User> Friends{get;set;}
  // other user properties
}

Why not just do

public class User
{
  public Guid UserId{get;set;}
  public string UserName{get;set;}
  public String UserAddress{get;set;}
  public IList<User> Friends{get;set;}
  // other user properties
}
虫児飞 2024-11-12 09:12:34

您需要自引用多对多关系,因为用户可以有许多朋友,也可以是许多用户的朋友。

public class User
{
    public Guid UserId { get; set; }
    public string UserName { get; set; }
    public String UserAddress { get; set; }

    [InverseProperty("Friends")]
    public virtual ICollection<User> FriendWith { get; set; }
    [InverseProperty("FriendWith")]
    public virtual ICollection<User> Friends { get; set;} 
}

或者您可以省略 InverseProperty 数据注释并使用流畅的映射:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
                .HasMany(u => u.Friends)
                .WithMany(f => f.FriendWith);
    // I'm not sure but you can also need to add this:
    //          .WillCascadeOnDelete(false);
}

You need self referencing many-to-many relation because use can have many friends but also can be friend of many users.

public class User
{
    public Guid UserId { get; set; }
    public string UserName { get; set; }
    public String UserAddress { get; set; }

    [InverseProperty("Friends")]
    public virtual ICollection<User> FriendWith { get; set; }
    [InverseProperty("FriendWith")]
    public virtual ICollection<User> Friends { get; set;} 
}

Or you can omit InverseProperty data annotation and use fluent mapping:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
                .HasMany(u => u.Friends)
                .WithMany(f => f.FriendWith);
    // I'm not sure but you can also need to add this:
    //          .WillCascadeOnDelete(false);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文