Inserting the DBSet collection to Mongodb as bson documents

c# entity-framework-core mongodb

Question

I am new to EntityFramework core.

I have a DBContext class which has all the DBSet objects. I would like to serialize all the objects to the MongoDB database. Is it possible to write a DBSet object to mongodb without converting them to a List object.

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Zeiss.IMT.MCCNeo.DataMigration.Utilities
{
    public partial class mccdbContext : DbContext
    {
        public virtual DbSet<AliveInfo> AliveInfo { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            optionsBuilder.UseSqlServer(@"Server=INBLRN0517\SQLEXPRESS;Database=mccdb;Trusted_Connection=True;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AliveInfo>(entity =>
            {
                entity.ToTable("ALIVE_INFO");

                entity.HasIndex(e => new { e.KmgId, e.System })
                    .HasName("UQ__ALIVE_IN__130B8EFC0D03FCC7")
                    .IsUnique();

                entity.Property(e => e.Id)
                    .HasColumnName("ID")
                    .ValueGeneratedNever();

                entity.Property(e => e.BdeEnabled).HasColumnName("BDE_ENABLED");

                entity.Property(e => e.FirstMsg).HasColumnName("FIRST_MSG");

                entity.Property(e => e.KmgId)
                    .IsRequired()
                    .HasColumnName("KMG_ID")
                    .HasColumnType("varchar(20)");

                entity.Property(e => e.LastMsg).HasColumnName("LAST_MSG");

                entity.Property(e => e.MessproEnabled).HasColumnName("MESSPRO_ENABLED");

                entity.Property(e => e.ObdEnabled).HasColumnName("OBD_ENABLED");

                entity.Property(e => e.System)
                    .IsRequired()
                    .HasColumnName("SYSTEM")
                    .HasColumnType("varchar(100)");
            });
    }
}

currently i am using this approach. I would like to do a bulk insert into mongodb all the records from the table ALIVE INFO.

class Program
    {
        static void Main(string[] args)
        {
            using (var mccdbContext = new mccdbContext())
            {
                var aliveInfoList = mccdbContext.AliveInfo.ToList();

                //converted list is written to mongodb.
            }

            Console.ReadKey();
        }
    }
1
2
8/22/2017 1:37:43 AM

Accepted Answer

DbSet inherits IEnumerable<TEntity> (https://msdn.microsoft.com/en-us/library/gg696460(v=vs.113).aspx). InsertMany in mongo c# driver accepts IEnumerable<TEntity>

so maybe you could write something like:

aliveInfoCollection.InsertMany(mccdbContext.AliveInfo as IEnumerable<AliveInfo>)
1
8/22/2017 9:22:20 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow