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();
}
}