[英]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: 工作流程如下:
User
. 这将创建一个User
。 SocialProvider
and adds it to user.SocialProviders
. 这将创建一个或多个SocialProvider
并将其添加到user.SocialProviders
。 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.