1

i have one json file and i deserialize this one in c# console entity frame work now i want to show data that was stored in the file to my table in sql server this is my code i tried to Equalize the fields in my file to my table fields what should i do next ?

 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

                };
            }

this is the code for deserialization

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

            return Student;
        }

this part works but i do not Know how to save this data to my data base and then show it ?

6
  • next thing you should do is add punctuation, because it is very confusing. Do you mean you want to store it in the database? where's the code for deserializing the json? and where's the code to save to the database? Commented Dec 3, 2019 at 13:03
  • yes i want to store it in data base Commented Dec 3, 2019 at 13:04
  • please update your question instead of putting your code in the comments. that's not very readable. if you want to add it to the database, just use context.Add(student1) Commented Dec 3, 2019 at 13:07
  • I did what you said sorry i am new in Programming and i just open my account that is why i did not know how to answer your question.context.add doesn't work here i used ado.net to bring my table from sql server. Commented Dec 3, 2019 at 13:10
  • I understand, but to prevent your question from being downvoted or removed, you need to be very clear, and add all relevant code. If you have a student context defined, you can simply add to the context, and then call savechanges on that context. that should do it. Commented Dec 3, 2019 at 13:13

1 Answer 1

1

You can do something like this:

// 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()
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.