简体   繁体   English

从SQL(C#和实体框架)到mongodb(Go和mgo)

[英]From SQL (C# & entity framework) to mongodb (Go and mgo)

I am coming today because I have to migrate from SQL (with entity framework) to MongoDb, however, the database side of programming is a field where I am not an expert and I would like to make the best choice for the evolution of the program I am working on. 我今天来是因为我必须从SQL(带有实体框架)迁移到MongoDb,但是,编程方面的数据库方面是我不是专家的领域,我想为程序的发展做出最佳选择我正在努力。

Let say I have this database schema (an idea, not the actual case) : 假设我有这个数据库模式(一个主意,而不是实际情况):

在此处输入图片说明

So I have a school. 所以我有一所学校。 In this school, I have classes and, those classes have students. 在这所学校,我有课,那些课有学生。 The foreign keys are the italic fields. 外键是斜体字段。

It should give something like that in C#: 它应该在C#中给出类似的内容:

public class SchoolEntity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; } = new Guid();

    public string Name { get; set; }

    [InverseProperty("School")]
    public virtual ICollection<ClassTeachingEntity> ClassTeachings { get; set; }
}

public class ClassTeachingEntity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; } = new Guid();

    public Guid SchoolId { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    [ForeignKey("SchoolId")]
    public virtual SchoolEntity School { get; set; }

    [InverseProperty("ClassTeaching")]
    public virtual ICollection<StudyingEntity> StudyingEntities { get; set; }
}

public class StudyingEntity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; } = new Guid();

    [Index("UniqueStudyingRelation", Order = 1, IsUnique = true)]
    public Guid UserId { get; set; }

    [Index("UniqueStudyingRelation", Order = 2, IsUnique = true)]
    public Guid ClassTeachingId { get; set; }

    [ForeignKey("UserId")]
    public virtual UserEntity User { get; set; }

    [ForeignKey("ClassTeachingId")]
    public virtual ClassTeachingEntity ClassTeaching { get; set; }
}

public class UserEntity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; } = new Guid();

    public string Firstname { get; set; }

    public string Lastname { get; set; }

    [InverseProperty("User")]
    public virtual ICollection<StudyingEntity> StudyingEntities { get; set; }
}

My question is, first, is the current structure good? 我的问题是,首先,当前结构是否良好? (I didn't designed it, it has been realized before I arrived and I am more a beginner than an expert in databases) (我没有设计它,它是在我到达之前就意识到的,我比数据库专家更像是一个初学者)

The second question is, do I have to create just one document from my four different tables? 第二个问题是,我是否必须从四个不同的表中仅创建一个文档? Because, as far as I know, in mongodb, it's a JSON logic, so I can store nested objects right? 因为据我所知,在mongodb中,这是一个JSON逻辑,因此我可以存储嵌套对象吗?

I don't know if this information matters but I will use Go with mongodb , not C# anymore. 我不知道这些信息是否重要,但是我将Go与mongodb一起使用,而不再是C#。

Thanks for your feedback 感谢您的反馈意见

If I understand you problem correctly you have to migrate existing C# + SQL program to GO + Mongo stack. 如果我正确理解了您的问题,则必须将现有的C#+ SQL程序迁移到GO + Mongo堆栈。 You are trying to address it asking technical questions like is the current structure good . 您正在尝试解决它,提出一些技术问题,例如is the current structure good

Something is missing here. 这里缺少一些东西。

Every technology is just a solution for business problem. 每种技术都只是解决业务问题的解决方案。 What is business problem here? 这是什么业务问题? Your question does not explain that. 您的问题不能解释这一点。

Try to get answers to questions below before we can move to technical side: 在转向技术方面之前,请尝试获取以下问题的答案:

  1. What is wrong with C# and SQL solution, so we have to replace it with something new? C#和SQL解决方案出了什么问题,因此我们必须用新的替换它吗?
  2. What does not work in existing RDBMS data model? 在现有的RDBMS数据模型中什么不起作用?

In any case be careful trying to apply your RDBMS and ORM patterns to mongo DB. 无论如何,都要小心尝试将RDBMS和ORM模式应用于mongo DB。 It is very different type of data storage (document DB) and RDBMS model cannot be just copied into it. 这是非常不同的数据存储类型(文档DB),RDBMS模型不能仅复制到其中。

1- The current structure seems ok. 1-当前的结构似乎还可以。 Of course, it will depend on your particular case, but it seems to be good. 当然,这取决于您的特定情况,但这似乎很好。

2- You can, but you don't have to. 2-您可以,但不必。 It makes sense to only have one document when your data is not relational. 当您的数据没有关系时,仅拥有一个文档是有意义的。 In your case, it probably makes more sense to keep them separated, otherwise you may have a lot of repeated entries. 就您而言,将它们分开可能更有意义,否则您可能会有很多重复的条目。 You can use the $lookup aggregation to perform joins between documents. 您可以使用$ lookup聚合在文档之间执行联接。

Just be careful with $lookup . 请注意$ lookup The last time I read about it, Mongo did not support this operation if your data was sharded. 我上一次阅读有关内容的信息时,如果将您的数据分片,则Mongo不支持此操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM