如何自动将名称 - 值对列表绑定到EF6模型对象,以避免必须手动显式分配每个字段?下面的代码是一个控制台应用程序,而不是MVC。
(我将数据从Excel电子表格导入数据库优先EF模型。目前我正在使用列号访问单元格值。我宁愿查找列标题并使用它来绑定相应的联系人字段。)
Contact contact = new Contact();
contact.ModelID = Model.ModelID;
contact.Email = Convert.ToString( sheet.Cells[rowNum, 1]);
contact.CreatedByID = Convert.ToInt32( sheet.Cells[rowNum, 2]);
contact.CreatedOn = Convert.ToDateTime( sheet.Cells[rowNum, 3]);
contact.CategoryID = Convert.ToInt32( sheet.Cells[rowNum, 4]);
contact.Company = Convert.ToString( sheet.Cells[rowNum, 5]);
contact.Phone = Convert.ToString( sheet.Cells[rowNum, 6]);
contact.Department = Convert.ToString( sheet.Cells[rowNum, 7]);
contact.OrganizationCode = Convert.ToString( sheet.Cells[rowNum, 8]);
contact.GivenName = Convert.ToString( sheet.Cells[rowNum, 9]);
contact.FamilyName = Convert.ToString( sheet.Cells[rowNum, 10]);
contact.Street = Convert.ToString( sheet.Cells[rowNum, 11]);
contact.PostalBox = Convert.ToString( sheet.Cells[rowNum, 12]);
contact.Town = Convert.ToString( sheet.Cells[rowNum, 13]);
contact.StateRegion = Convert.ToString( sheet.Cells[rowNum, 14]);
contact.PostalCode = Convert.ToString( sheet.Cells[rowNum, 15]);
contact.Country = Convert.ToString( sheet.Cells[rowNum, 16]);
db.Contacts.Add(contact);