There is a database with related tables. Entity Framework Code First

Table 1.

public class Student { public Student() { CourseWork = new ObservableCollection<CourseWork>(); StateAtt = new ObservableCollection<StateAttestation>(); AddInfo = new ObservableCollection<AdditionalInformation>(); Practice = new ObservableCollection<Practice>(); Discipline = new ObservableCollection<Discipline>(); Electives = new ObservableCollection<Electives>(); } private ObservableCollection<CourseWork> _courseWork; private ObservableCollection<StateAttestation> _stateAtt; private ObservableCollection<AdditionalInformation> _addInfo; private ObservableCollection<Practice> _practice; private ObservableCollection<Discipline> _discipline; private ObservableCollection<Electives> _electives; public virtual ObservableCollection<CourseWork> CourseWork { get { return _courseWork; } set { _courseWork = value; } } public virtual ObservableCollection<StateAttestation> StateAtt { get { return _stateAtt; } set { _stateAtt = value; } } public virtual ObservableCollection<AdditionalInformation> AddInfo { get { return _addInfo; } set { _addInfo = value; } } public virtual ObservableCollection<Practice> Practice { get { return _practice; } set { _practice = value; } } public virtual ObservableCollection<Discipline> Discipline { get { return _discipline; } set { _discipline = value; } } public virtual ObservableCollection<Electives> Electives { get { return _electives; } set { _electives = value; } } [Key] public int StudentID { get; set; } [Required] public string FullName { get; set; } [Required] public DateTime BirthDate { get; set; } [MaxLength(265)] public string PreviousLevelEducation { get; set; } public int RegistrationNumber { get; set; } [Required] public DateTime ExtraditionDate { get; set; } [Required] public bool ExcellentAttribute { get; set; } [Required] public int DiplomSeries { get; set; } [Required] public int DiplomNumber { get; set; } [Required] public int AttachmentSeries { get; set; } [Required] public int AttachmentNumber { get; set; } [Required] public DateTime DecisionDate { get; set; } [Required] public string Qualification { get; set; } [Required] public string Specialty { get; set; } [Required] public string Lifetime { get; set; } } 

Table 2.

  public class Discipline { [Key] public int DisciplineID { get; set; } [Required] public string DisciplineName { get; set; } [Required] public int QuantityHours { get; set; } [InverseProperty("Discipline")] public virtual Student discipline { get; set; } [Required] public string Rating { get; set; } } 

How to implement copying a row from the main (table 1) and all related rows from table 2 and the rest? I understand that you need to replace all the keys, but I haven’t found any implementation on the Internet. (Copy to the same tables, but with different id)

  • Copy where? What does the replacement of keys? - Alexander Petrov
  • Copy to the same tables, but with different id - Dmitriy Shevchenko
  • Копировать в те же таблицы, но с другими id is an important remark to add to the question! - Bulson
  • @Bulson Posted by - Dmitriy Shevchenko
  • In other words, you want to create duplicate records, or clones in the database. Usually they are trying to get rid of them, and not intentionally create them. It seems to me that this is easier to do directly in the DBMS using SQL, and not to write a program for this. But if you really need, then you just need to read interrelated data from the tables, then do DeepCloning entities (copy all properties, except for ID), and then again record these new clones in the database as normal new data, and IDs will assigned by machine when recording. There may be problems if there are demands on the uniqueness of the columns - Bulson

0