使用 C# 显示数据库中 json 文件的数据

showing data from a json file in database using C#

我有一个 json 文件,我在 c# 控制台实体框架工作中反序列化了这个文件,现在我想在 sql 中向我的 table 显示存储在文件中的数据服务器 这是我的代码,我试图将我文件中的字段与我的 table 字段相等化我接下来应该做什么?

 using (var context = new UNIEntities1())
            {
                var majorId = context.Major.Where(e =>e.MajorName == dto.Major).Select(e => e.IdMajor);
                return new student1()
                {
                    StudentName = dto.StudentName,
                    Age = dto.Age,
                    Address = dto.Address,
                    Major = majorId,
                    PhoneNumber = dto.PhoneNumber

                };
            }

这是反序列化的代码

 public static StudentDto Deserialize()
        {
            var Student = JsonConvert.DeserializeObject<StudentDto>(File.ReadAllText(@"unii.json"));

            return Student;
        }

这部分有效,但我不知道如何将此数据保存到我的数据库然后显示它?

你可以这样做:

// get the deserialize object
var dto = Deserialize(); 

// create a new instance of Student from the dto
var newStudent = new Student
{
    StudentName = dto.StudentName,
    Age = dto.Age,
    Address = dto.Address,
    Major = dto.Major,
    PhoneNumber = dto.PhoneNumber
}; 

// save it to the database
using (var context = new UNIEntities1())
{
    context.Student.Add(newStudent);
    context.AcceptChanges(); // or SaveChanges()
}