LINQ to SQL - adding a new record

I have a database table with an auto-increment field. this table is exposed to code through an entity data model. I am trying to write a new entry to this table. I have a method that should be responsible for creating these records. This method accepts record property values. He must create a record and record a control record in another table. I am currently trying

public int CreateRecord(string name, string description, List<int> ids)
{
  using (DatabaseContext database = new DatabaseContext())
  {
    Record record = new Record();
    record.Name = name;
    record.Description = description;

    database.Records.InsertOnSubmit(record);
    database.SubmitChanges();

    List<RecordTrack> tracks = new List<RecordTrack>();
    foreach (int id in ids)
    {
      RecordTrack track = new RecordTrack();
      track.RecordID = record.ID;
      track.ID = id;
      tracks.Add(track);
    } 
    database.Tracks.InsertAllOnSubmit(tracks);
    database.SubmitChanges();
  }
}

I can't seem to record this way. I was able to do this when I transferred the record to which I wanted to insert. But due to other factors, I need a way to cleanly create a record here from scratch. What am I doing wrong?

Thank!

+5
source share
1 answer

AddToRecord() . record, SaveChanges() .

+1

All Articles