Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架-获取关注者帖子_C#_Entity Framework_Ef Code First_Entity Framework 4_Ef Fluent Api - Fatal编程技术网

C# 实体框架-获取关注者帖子

C# 实体框架-获取关注者帖子,c#,entity-framework,ef-code-first,entity-framework-4,ef-fluent-api,C#,Entity Framework,Ef Code First,Entity Framework 4,Ef Fluent Api,我已经用实体框架和fluentapi编写了一个restapi。在这方面,我想得到我的追随者创造的职位。我已检索到追随者列表,但也希望他们的帖子 我的用户实体和帖子实体如下: public class User { public string Id { get; set; } public string Email { get; set; } public string Password { get; set; } } public class BlogPost {

我已经用实体框架和fluentapi编写了一个restapi。在这方面,我想得到我的追随者创造的职位。我已检索到追随者列表,但也希望他们的帖子

我的用户实体和帖子实体如下:

public class User
{
    public string Id { get; set; }

    public string Email { get; set; }

    public string Password { get; set; }
}

public class BlogPost
{
    public string Summary { get; set; }

    public string Body { get; set; }

    public User CreatedBy { get; set; }
}
public class UserRelationship
{
    public User UserId { get; set; }

    public User Follower { get; set; }
}
以及用户之间的追随者/以下关系,如下所示:

public class User
{
    public string Id { get; set; }

    public string Email { get; set; }

    public string Password { get; set; }
}

public class BlogPost
{
    public string Summary { get; set; }

    public string Body { get; set; }

    public User CreatedBy { get; set; }
}
public class UserRelationship
{
    public User UserId { get; set; }

    public User Follower { get; set; }
}

现在我怎样才能找回我追随者的帖子呢?或者我必须更改结构本身吗?

保留当前的模型结构,您需要为关注者创建一个get-on-BlogPost实体。这将为您提供来自追随者的帖子。然而,这将增加对数据库的额外访问以及服务器端代码的一些翻译

在我看来,模式应该有所不同。用户是父实体,它也应该有博客文章的详细信息。考虑到基于文档的NoSQL数据库,用户很容易识别为聚合,因此它必须能够方便应用程序可能需要的任何遍历

因此,在代码中,我更喜欢这样的内容:

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

    public string Name
    {
        get; set;
    }

    public ICollection<BlogPost> Blogs
    {
        get; set;
    }

    public ICollection<User> Followers
    {
        get; set;
    }
}

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

    public string Title
    {
        get; set;
    }

    public string Content
    {
        get; set;
    }

    public User CreatedBy
    {
        get; set;
    }
}
公共类用户
{
公共整数Id
{
获得;设置;
}
公共字符串名
{
获得;设置;
}
公共ICollection博客
{
获得;设置;
}
公共ICollection追随者
{
获得;设置;
}
}
公共类博客文章
{
公共整数Id
{
获得;设置;
}
公共字符串标题
{
获得;设置;
}
公共字符串内容
{
获得;设置;
}
公共用户CreatedBy
{
获得;设置;
}
}

在数据库方面,您需要使用映射表方法来建立用户之间的多对多关系。

感谢您的快速回复。使用它,我可以获得个人用户的帖子,但我想获得所有追随者的帖子,就像在twitter上一样。这个模型也适用吗?@itdeeps我不知道twitter是如何工作的,但是如果你填充followers属性,你可以获得所有追随者的所有帖子。你将加载追随者的用户和博客详细信息,这将减少你的应用程序的聊天。我相信在任何时候向客户端发送的数据都比服务器发送的数据要多。但是,您需要自己评估适合您的最佳消息大小和详细信息。