简体   繁体   English

我需要在两个代码优先的实体之间创建一个复杂的关系,但是我很难缠着它

[英]I need to create a complex relationship between two code-first entities but I'm having a hard time wrapping my brain around it

I have two entities and these two entities have two different relationships between one another. 我有两个实体,这两个实体之间有两个不同的关系。 The first relationship is a one to many relationship and the second relationship is a many to many relationship. 第一个关系是一对多关系,第二个关系是多对关系。 See the entities below. 请参阅下面的实体。

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

The workflow is as follows: 工作流程如下:

  1. I sign up within the application. 我在应用程序中注册。 This creates a User . 这将创建一个User
  2. I attach social providers (Google, Facebook, etc). 我附有社交服务提供商(Google,Facebook等)。 This creates one or more SocialProvider and adds it to user.SocialProviders . 这将创建一个或多个SocialProvider并将其添加到user.SocialProviders
  3. I import my contacts/friends from my attached social providers. 我从附加的社交服务提供商处导入我的联系人/朋友。 This pulls down a list of social contacts and creates a SocialProvider for each one if it does not already exist and adds them to user.SocialContacts . 这会下拉一个社交联系人列表,并为每个社交联系人(如果尚不存在)创建一个SocialProvider并将其添加到user.SocialContacts In this case the SocialProvider will not have a parent user that "owns" it, nor will it have an AccessToken until a user signs up with the application and adds a social provider that matches it, in which case those things will be populated. 在这种情况下, SocialProvider将不会具有“所有者”它的父用户,也不会具有AccessToken直到用户注册该应用程序并添加与之匹配的社交提供程序为止,在这种情况下,这些东西将被填充。

Will this work? 这样行吗? I have a feeling EF is about to get very confused. 我感到EF即将变得非常困惑。 I'm not very familiar with the fluent API or even most of the data annotations. 我对流畅的API甚至大多数数据注释不是很熟悉。 Any suggestions for what I can do to make this work, or should I just make SocialContacts be a new entity called SocialContact that mostly looks like SocialProvider just to avoid any confusing relationship stuff? 关于我可以做些什么的任何建议,还是应该让SocialContacts成为一个名为SocialContact的新实体,该实体看起来像SocialProvider只是为了避免造成任何混乱的关系?

Try this: 尝试这个:

User 用户

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

SocialProvider 社会提供者

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

Then in your DbContext class 然后在您的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
          });
    }

I can't and haven't test this, so let me know the results so i can improve it. 我不能也没有进行测试,所以让我知道结果,以便我可以改善它。 Ideally, you may have to pick this design apart and restructure it into one or more entities. 理想情况下,您可能必须将这种设计分开,并将其重组为一个或多个实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用EF4 Code-First一次创建两个关联实体 - How do I create two associated entities at one time with EF4 Code-First 如何以EF6代码优先的方式删除实体之间的关系? - How can I remove relationship between entities in EF6 code-first? C# 中的私有“设置” - 无法将我的大脑围绕在它周围 - Private 'set' in C# - having trouble wrapping my brain around it 我需要定义这两个实体之间的任何关系吗? - Do I need to define any relationship between these two entities? 如何在Entity Framework Code-First中创建自定义M2M表 - How can I create a custom m2m table in Entity Framework Code-First 如何使3个实体之间的一对多关系与Entity Framework Core 2.1一起使用 - 代码优先 - How to make one-to-many relationship between 3 entities work with Entity Framework Core 2.1 - code-first 在将live ID与我的MVC网站集成时,我很难过 - I'M having hard time while integrating live ID with my MVC Website 首先使用Entity Framework代码创建复杂的主键 - Create a complex Primary Key using Entity Framework code-first EF代码优先1:相同模型之间的1:* 1:0..1关系 - EF code-first 1:* 1:0..1 relationship between the same models 我可以使用并返回EF4代码优先的POCO实体作为其接口吗? - Can I use and return EF4 code-first POCO entities as their interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM