locked
Can you create a new entity without setting its links? RRS feed

  • Question

  • Say I have a "Contact" table in MS SQL which has relationships with other tables either via a mapping table or directly, and then I generate the EDM and expose it as a Data Service. Now I wish to write some basic test code for creating a new contact and pushing it to the DB. Must I also populate all the relationship links?

    I have this SQL table:

    ContactID, int, autoincrement, no-null
    Title, nvarchar, yes-null
    FirstName, nvarchar, no-null
    MiddleName, nvarchar, yes-null
    LastName, nvarchar, no-null
    Suffix, nvarchar, yes-null
    Birthdate, datetime, no-null
    IsActive, bit, no-null
    rowguid, (newguid()), no-null
    CreatedDate, datetime, (getdate()), no-null
    ModifiedDate, datetime, (getdate()), no-null


    This table is connected to several others (such as "Address", or "Phone" via mapping tables using the corresponding ID columns from each table).

    I use the following test code to try to create a new Contact without setting any of its links:

    Gateway gateway = new Gateway();
    contact = new Contact();
    contact.FirstName = "John";
    contact.LastName = "Doe";
    contact.Birthdate = new DateTime(2000, 1, 1);
    contact.IsActive = true;
    gateway.AddContact(contact);

    Here's the other relevant code:

    public Gateway()
    {
        Uri serviceUri = new Uri("http://localhost:50000/DataService.svc");
        context = new DataModel(serviceUri);
        context.MergeOption = MergeOption.AppendOnly;
    }

    public void AddContact(Contact contact)
    {
        context.AddObject("Contact", contact);
        DataServiceResponse response = context.SaveChanges();
    }


    The response says HasErrors = true and under "Results View" in the debugger, my StatusCode is 7. I can't quite figure out what I'm doing wrong. I'm wondering if the fact that I haven't generated any of the relationship links is the issue or if it's some other problem.

    Thanks for the help.
    Wednesday, August 6, 2008 3:58 PM

Answers

  • Yup you got it, no need to set links.  The requirement for links to be present is service specific and depends on the constrainst applied by the service on its data model.  In the database case if FK's are nullable then no links required.  If there were not nullable, then one would have to supply the link information.

    Tuesday, August 12, 2008 4:17 PM

All replies

  • I think I've figured out what I'm doing wrong. Indeed, I've discovered that you don't need to set any links of an entity prior to saving it to the database (which makes sense, since links aren't mandetory on each row in a table).

    The problem is: not manually setting the CreatedDate, ModifiedDate, and rowguid values in code prior to saving the entity. I was under the impression that SQL would automatically set those values since their "initial values" were set as either "getdate()" or
    "newguid()". Once I set those fields using DateTime.Now and Guid.NewGuid(), and then saved the entity, everything worked.
    Wednesday, August 6, 2008 6:55 PM
  • Yup you got it, no need to set links.  The requirement for links to be present is service specific and depends on the constrainst applied by the service on its data model.  In the database case if FK's are nullable then no links required.  If there were not nullable, then one would have to supply the link information.

    Tuesday, August 12, 2008 4:17 PM