C# 在.NETCore中建模mongodb存储的引用关系

C# 在.NETCore中建模mongodb存储的引用关系,c#,mongodb,asp.net-core-mvc,C#,Mongodb,Asp.net Core Mvc,我是mongo的新手,在处理引用关系和在.NETCore中对其建模时,我正在寻找一些关于最佳实践的指导 是的,这是常见的“加入mongodb?”问题。但我还没有找到一个很好的答案 为了简单起见,假设我有一个简单的API,我正在用控制器构建它来管理用户和帐户 在mongo中有两个集合:帐户和用户。用户属于其父帐户。在这种情况下,我不想使用嵌入式文档路径,因此每个用户文档中都有一个AccountID,用于将用户链接回其父帐户 我在.net中的当前实体是: public class User {

我是mongo的新手,在处理引用关系和在.NETCore中对其建模时,我正在寻找一些关于最佳实践的指导

是的,这是常见的“加入mongodb?”问题。但我还没有找到一个很好的答案

为了简单起见,假设我有一个简单的API,我正在用控制器构建它来管理用户和帐户

在mongo中有两个集合:帐户和用户。用户属于其父帐户。在这种情况下,我不想使用嵌入式文档路径,因此每个用户文档中都有一个AccountID,用于将用户链接回其父帐户

我在.net中的当前实体是:

public class User
{

    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    [BsonElement("firstName")]
    public string FirstName { get; set; }

    [BsonElement("lastName")]
    public string LastName { get; set; }

    [BsonElement("email")]
    public string Email { get; set; }

    [BsonElement("status")]
    public string Status { get; set; }

    [BsonElement("type")]
    public string Type { get; set; }

    [BsonElement("createdDateTime")]
    public DateTime CreatedDateTime { get; set; }

    [BsonElement("modifiedDateTime")]
    public DateTime ModifiedDateTime { get; set; }

    [BsonRepresentation(BsonType.ObjectId)]
    [BsonElement("accountId")]
    public string AccountId { get; set; }

}

 public class Account
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    [BsonElement("name")]
    public string Name { get; set; }

    [BsonElement("status")]
    public string Status { get; set; }

    [BsonElement("type")]
    public string Type { get; set; }

    [BsonElement("createdDateTime")]
    public DateTime CreatedDateTime { get; set; }

    [BsonElement("modifiedDateTime")]
    public DateTime ModifiedDateTime { get; set; }

}
然后使用控制器中的AutoMapper将这些映射到模型

public class UserModel
{

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    [Required]
    public string Email { get; set; }

    [Required]
    public string Status { get; set; }

    [Required]
    public string Type { get; set; }

    [Required]
    public DateTime CreatedDateTime { get; set; }

    [Required]
    public DateTime ModifiedDateTime { get; set; }

    [Required]
    public string AccountId { get; set; }

}

public class AccountModel
{

    [Required]
    public string Name { get; set; }

    [Required]
    public string Status { get; set; }

    [Required]
    public string Type { get; set; }

    [Required]
    public DateTime CreatedDateTime { get; set; }

    [Required]
    public DateTime ModifiedDateTime { get; set; }

}
以及使用映射器的控制器方法的示例:

[HttpGet]
    public async Task<ActionResult<List<AccountModel>>> Get()
    {
        try
        {
            var results = await _repository.Get();

            return _mapper.Map < List < AccountModel >>(results);
        }
        catch (Exception ex)
        {
            return this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure");
        }


    }
财产

       [BsonRepresentation(BsonType.ObjectId)]
    [BsonElement("accountId")]
    public string AccountId { get; set; }
仍将保留在用户实体中,因此将保留引用

然后,用户模型可以具有如下属性

 public string AccountName { get; set; }
它们使用映射器填充


当您希望引用相关对象而不是嵌入它们时,这是设置此设置的最佳方法吗?这里有我遗漏的一些问题吗?

看看下面的代码。它使用我的库,它内置支持实体之间的一对一、一对多和多对多关系

使用MongoDB.Entities;
使用System.Linq;
命名空间堆栈溢出
{
公共课程
{
公共类帐户:实体
{
公共字符串名称{get;set;}
公共多用户{get;set;}
公共帐户()=>this.InitOneToMany(()=>用户);
}
公共类用户:实体
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公用一个帐户{get;set;}
[忽略]
公共字符串AccountName{get;set;}
}
私有静态void Main(字符串[]args)
{
新DB(“测试”);
var account=新帐户{Name=“父帐户”};
account.Save();
var user=新用户
{
FirstName=“dave”,
LastName=“mathews”,
Account=Account.ToReference()
};
user.Save();
account.Users.Add(用户);
//按ID查找父项
var parent=DB.Find().One(account.ID);
//获取父级的第一个用户
var dave=parent.Users.ChildrenQueryable()
.FirstOrDefault();
//得到戴夫的帐户
var davesAccount=dave.Account.ToEntity();
//使用单个mongo查询填写的帐户名获取dave
var daveExtra=(来自DB.Queryable()中的u,其中(u=>u.ID==dave.ID)
在u.Account.ID等于a.ID的DB.Queryable()中加入a
选择新用户
{
ID=u.ID,
FirstName=u.FirstName,
LastName=u.LastName,
AccountName=a.Name
}).SingleOrDefault();
}
}
}
 public string AccountName { get; set; }