dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] EFW add new object

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    258

    Resolved [RESOLVED] EFW add new object

    Hi All,

    I have been off programming for a while, and now trying to get back on a track. My understanding of Entiti frame work for adding a new object is by calling the AddTo method

    Code:
    DBContect.AddToEntityName(new ObjectName(){})
    then i realised somehow it adds newly initiated instances without calling the AddTo method, see code below

    Code:
    DB = new DBEntities(EFWConStr);
    Invoice INV = new Invoice() { Contractor = _JOB.Contractor, AccountingDate = DateTime.Now, DueDate = DateTime.Now.AddMonths(1), Notes = "", JobId = _JOB.ID, Ref = oCom.MyCompany.NextInvRef.ToString() };
    DB.SaveChanges();
    when i run this code, a new record is added to database. Don't understand how?! help please

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,788

    Re: EFW add new object

    Relationships are two-way in EF so if '_JOB.Contractor' is an entity in your EF context then assigning that to a property of your new object creates a relationship between the two and pulls your object into that context.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    258

    Re: EFW add new object

    Ok I think i worked it out, I have 1t to many relationship ( Contractor to Invoices) and the invoice object is being added to contractor invoices collection and saved there after. I swaped the contractor property with ContractorID and did the trick..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width