0

我有两个实体,这两个实体之间有两种不同的关系。第一个关系是一对多关系,第二个关系是多对多关系。请参阅下面的实体。

public class User
{
    public int Id { get; set; }
    public string Password { get; set; }
    public string Name { get; set; }

    // Collection that represent this user's attached social providers.
    public virtual ICollection<SocialProvider> SocialProviders { get; set; }
    // Collection representing this user's friends from her attached social providers.
    public virtual ICollection<SocialProvider> SocialContacts { get; set; }
}

public enum Provider
{
    Google,
    Facebook,
    Twitter
}

public class SocialProvider
{
    public int Id { get; set; }
    public Provider Provider { get; set; }
    // Granted access token for talking to the provider API on user's behalf.
    public string AccessToken { get; set; }
    // User's unique ID at the social provider.
    public string ProviderId { get; set; }

    // The user that this SocialProvider belongs to.
    public virtual User User { get; set; }
    // The users that have this social provider as a contact.
    public virtual ICollection<User> Contacts { get; set; }
}

工作流程如下:

  1. 我在应用程序中注册。这将创建一个User.
  2. 我附上社交服务提供商(谷歌、Facebook 等)。这会创建一个或多个SocialProvider并将其添加到user.SocialProviders.
  3. 我从我附属的社交提供商那里导入我的联系人/朋友。这会拉下社交联系人列表并SocialProvider为每个人创建一个(如果它尚不存在)并将它们添加到user.SocialContacts. 在这种情况下,SocialProvider不会有父用户“拥有”它,也不会有父用户,AccessToken直到用户注册应用程序并添加与之匹配的社交提供者,在这种情况下,这些东西将被填充。

这行得通吗?我有一种感觉 EF 即将变得非常困惑。我对 fluent API 甚至大部分数据注释都不是很熟悉。关于我可以做些什么来完成这项工作的任何建议,或者我应该只是SocialContacts成为一个名为的新实体SocialContact,主要看起来SocialProvider只是为了避免任何令人困惑的关系内容?

4

1 回答 1

1

尝试这个:

用户

public class User
{
   public int Id { get; set; }
   ublic string Password { get; set; }
   public string Name { get; set; }

   // Collection that represent this user's attached social providers.
   public virtual ICollection<SocialProvider> SocialProviders { get; set; }

   // Collection representing this user's friends from her attached social providers.
   public virtual ICollection<SocialProvider> SocialContacts { get; set; }
}

社交提供者

public class SocialProvider
{
   public int Id { get; set; }        

   public string AccessToken { get; set; }       

   [ForeignKey("Provider"), DatabaseGenerated(DatabaseGeneratedOption.None)]
   public string ProviderId { get; set; }

   public Provider Provider { get; set; }

   [ForeignKey("User"), DatabaseGenerated(DatabaseGeneratedOption.None)]
   public string UserId { get; set; }

   // The user that this SocialProvider belongs to.
   public virtual User User { get; set; }

   // The users that have this social provider as a contact.
   public virtual ICollection<User> Contacts { get; set; }
}

然后在你的 DbContext 类

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
          .HasMany<SocialProvider>(r => r.SocialContacts)
          .WithMany(u => u.Contacts)
          .Map(m =>
          {
              m.ToTable("UsersInSocialContacts");
              m.MapLeftKey("UserId");
              m.MapRightKey("ProviderId");//If not ProviderId  try Id
          });
    }

我不能也没有测试这个,所以让我知道结果,以便我改进它。理想情况下,您可能必须分离此设计并将其重组为一个或多个实体。

于 2013-04-17T01:31:49.923 回答