C# EF-一对多关系中的一对多关系(Facebook帖子/反应)
我拥有以下实体:C# EF-一对多关系中的一对多关系(Facebook帖子/反应),c#,.net,entity-framework,ado,C#,.net,Entity Framework,Ado,我拥有以下实体: public class User { public User() { UserName = new Name(); UserEmail = new Email(); } [Key] public int Gid { get; set; } public Name UserName { get; set; } public Email UserEmail { get; set; }
public class User
{
public User()
{
UserName = new Name();
UserEmail = new Email();
}
[Key]
public int Gid { get; set; }
public Name UserName { get; set; }
public Email UserEmail { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public int UserId { get; set; }
public string Content { get; set; }
public string ImageUri { get; set; }
public virtual User Author { get; set; }
}
public class Reaction
{
public int ReactionId { get; set; }
public string Text { get; set; }
public string IconUri { get; set; }
}
公共类用户
{
公共用户()
{
用户名=新名称();
UserEmail=新电子邮件();
}
[关键]
公共int Gid{get;set;}
公共名称用户名{get;set;}
公共电子邮件用户电子邮件{get;set;}
公共虚拟ICollection Posts{get;set;}
}
公营职位
{
公共int PostId{get;set;}
public int UserId{get;set;}
公共字符串内容{get;set;}
公共字符串ImageUri{get;set;}
公共虚拟用户作者{get;set;}
}
公众阶级反应
{
public int ReactionId{get;set;}
公共字符串文本{get;set;}
公共字符串IconUri{get;set;}
}
一个用户可以有多个帖子,而一个帖子可以有多个反应问题是,一个反应应该存储对其帖子和作出反应的用户的引用。我可以在用户和帖子之间建立一对多的关系
如何使用实体框架映射此关系?在结尾添加注释后添加 如果您遵循一对多关系,则不必添加任何属性,也不必使用fluentapi告诉实体框架您想要什么 只有当您需要不同的表名、属性类型、列名或表之间关系的其他特性时,才需要属性或fluent API 您的问题是因为您在类定义中使用了一些一对多定义造成的 您的用户:
public class User
{
public int Id { get; set; }
// every user has zero or more Posts (one-to-many)
public virtual ICollection<Post> Posts { get; set; }
...
}
最后是您的DbContext:
public MyDbContext : DbContext
{
public DbSet<User> Users {get; set;}
public DbSet<Post> Posts {get; set;}
public DbSet<Reaction> Reactions {get; set;}
}
评论后添加内容 如果您想要一个“有所有反应的用户”,请使用
选择many
。这实际上是一个LINQ问题,与实体框架无关
var usersWithAllTheirReactions = myDbContext.Users
.Where (user => ...)
.Select(user => new
{
Id = user.Id,
Name = ...,
AllReactions = user.Posts
.SelectMany(post => post.Reactions)
.Where(reaction => ...)
.Select(reaction => new
{
ReactionDate = reaction.Date,
Text = reaction.Text,
}),
}),
});
有没有可能让反应指向对帖子做出反应的用户?有可能,但你的数据库将不再被规范化(谷歌为什么数据库规范化很重要)。如果您想要“拥有所有参考资料的用户”,请使用
SelectMany
public MyDbContext : DbContext
{
public DbSet<User> Users {get; set;}
public DbSet<Post> Posts {get; set;}
public DbSet<Reaction> Reactions {get; set;}
}
var oldUsersWithManyReactions = myDbContext.Users
.Where(user => user.BirthDay < new DateTime(2040, 1, 1))
.Select(user => new
{
// Select only the user properties you plan to use
Id = user.Id,
FullName = user.Name.First + User.Name.Last,
// select the Posts of this User:
RecentPosts = user.Posts
.Where(post.PublicationDate > DateTime.UtcNow.AddDay(-7))
.Select(post => new
{
// again select only the Properties you want to use:
Title = post.Title,
PublicationDate = post.PublicationDate,
ReactionCount = post.Reactions.Count(),
}),
}),
}),
});
var usersWithAllTheirReactions = myDbContext.Users
.Where (user => ...)
.Select(user => new
{
Id = user.Id,
Name = ...,
AllReactions = user.Posts
.SelectMany(post => post.Reactions)
.Where(reaction => ...)
.Select(reaction => new
{
ReactionDate = reaction.Date,
Text = reaction.Text,
}),
}),
});