LINQ to SQL

While trying to use LINQ to SQL, I ran into several problems.

I have table faces:

  • int ID
  • string firstName
  • string lastName

And tabular notes that have:

  • int ID
  • string noteText
  • createdBy string
  • datetime creationDate
  • int PersonID

PersonID is a foreign key, and the ratio is 1: n

I tried using LINQ to SQL to create a person and some notes for each person.

Person person = new person(); Person.firstName = "me"; Person.note = new note(); Person.note.noteText = "some textโ€ฆ"; _DataContext.Persons.InsertOnSubmit(person); _DataContext.SubmitChanges(); 

The problem is that the human object does not yet exist in the database, so it does not yet have an identifier. Thus, the note.personID file has the value 0 ... (the ID field is the identification field on the sql server)

The only solution I found for this is to create a person, submit, and then re-create a note and sign.

Am I missing something here, or perhaps the way I need to work with LINQ to SQL?

How to add a few notes per person using LTS? I have a 1: n ratio and I do not see it with LTS.

If a person has 10,000 notes, I donโ€™t want the constructor of the personโ€™s object to load all the records that he has. I want to download them only when I access them. How can I configure LTS to load notes upon request?

+3
source share
1 answer

If you are not using the LinqToSql class constructor, you should consider using it. The created classes will support the insert script you described.

I can say that you are not using a constructor because it will give you the Notes property (plural) in Person ... like Person from 1 to Many with Notes.

How to configure LTS to download notes on demand?

The constructor will generate a property of type EntitySet (Note), which will load Notes upon request.

+3
source

All Articles