public static void InsertData() { string tableName1 = "tbClass"; string tableName2 = "tbStudent"; string[] classNames = { "A", "B", "C", "D", "E", "F" }; string[] studentNames = { "Maysam", "Iman", "Amir", "Reza", "Morteza", "Hamid", "Abolfazl", "Mohamad", "Hasan", "Saeed" }; string[] teacherNames = { "Saeedi", "hosayni", "Sasani", "Mansori", "Babaie", "Ashrafi" }; var ctx = new DadeAfzaMongoDbContext(); ctx.OpenDatabase("Db_1"); var table1 = ctx.Database.GetCollection <MongoDynamicRecord>(tableName1); var table2 = ctx.Database.GetCollection <MongoDynamicRecord>(tableName2); Random rand = new Random(); for (int i = 1; i <= 6; i++) { MongoDynamicRecord ClassEntity = new MongoDynamicRecord() { Id = i, Data = new BsonDocument(new Dictionary <string, object>() { { "Name", classNames[i - 1] }, { "Capcity", rand.Next(25, 35) }, { "TeacherName", teacherNames[i - 1] } } ) }; table1.InsertOne(ClassEntity); } for (int i = 1; i < 210; i++) { MongoDynamicRecord StudentEntity = new MongoDynamicRecord() { Id = i, Data = new BsonDocument(new Dictionary <string, object>() { { "Name", studentNames[rand.Next(0, 9)] }, { "Age", rand.Next(1, 92) }, { "Gender", rand.Next(0, 1) }, { "ClassId", rand.Next(1, 6) }, } ) }; table2.InsertOne(StudentEntity); } }
public static void Insert(string TableName, dynamic data) { //dynamic data1 = new ExpandoObject(); //data1.Id = "sgdvbfskdbfksdf"; //data1.Name = $"Name {-1}"; //data1.Age = 35; //Insert("Tb1", data1); //for (int i = 0; i < 100; i++) //{ // dynamic data = new ExpandoObject(); // data.Id = i; // data.Name = $"Name {i}"; // data.Age = i; // Insert(TableName, data); //} var table = db.GetCollection <MongoDynamicRecord>(TableName); MongoDynamicRecord de = new MongoDynamicRecord(); var dataDic = data as IDictionary <string, object>; de.Id = dataDic["Id"]; dataDic.Remove("Id"); // de.Id = idx++; de.Data = new BsonDocument(dataDic); table.InsertOne(de); }