简体   繁体   中英

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.

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#:

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?

I don't know if this information matters but I will use Go with mongodb , not C# anymore.

Thanks for your feedback

If I understand you problem correctly you have to migrate existing C# + SQL program to GO + Mongo stack. You are trying to address it asking technical questions like 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?
  2. What does not work in existing RDBMS data model?

In any case be careful trying to apply your RDBMS and ORM patterns to mongo DB. It is very different type of data storage (document DB) and RDBMS model cannot be just copied into it.

1- The current structure seems ok. Of course, it will depend on your particular case, but it seems to be good.

2- You can, but you don't have to. 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.

Just be careful with $lookup . The last time I read about it, Mongo did not support this operation if your data was sharded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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