R
RichB
I posted a previous question regarding creation of Collections in LinqtoSql
and a premature question related to editing objects. I thought that I had
managed to sort it out, but unfortunately I haven't.
It may be useful to look at the following post as it was my stating point
and provides a solution which I have implemented:
http://www.microsoft.com/communitie...40bca7c0&lang=en&cr=US&sloc=en-us&m=1&p=1#top
This solution works perfectly for creating records, however when it comes to
updating those records the behavior is strange and I cannot work out why.
I am using Linq2Sql to generate the model returned by a repository pattern
(repo.Get(id)). That Venue repository contains within it's structure the
EntitySet<ContactData> _contactDatas as a variable within a ContactList.
I have two cases both use a database table (ContactData) with an id column
and a fk to the id column of another table (ContactList) thereby giving a
ContactList = 1 to ContactData = many relationship:
Example 1- my demo project
ContactData id column is of type int IDENTITY. The FK to ContactList Allows
Nulls (for example only)
Example 2 - my actual project
ContactData id column is of type uniqueinteger, is a RowGuid and has a
default value = newid(). The FK to ContactList does not allow nulls. Within
Linq2Sql designer AutoGenerated Value = true.
My Controller code to handle a edit post is as follows:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Venue venue = repo.GetVenue(id);
UpdateModel(venue, "Venue", formValues.ToValueProvider());
repo.Save();
Trace.WriteLine(venue.VenueDetail.ContactLink.ContactDatas.ToString());
//However it is not populated with objects
Trace.WriteLine(venue.VenueDetail.ContactLink.ContactDatas[0].ToString());
return RedirectToAction("Edit", new { id = venue.VenueId });
}
For Example 1:
When I load the venue with the model data I have a ContactData object within
the ContactList. If I then step into the UpdateModel(...) method the venue is
updated with data from the formvalues collection. If I update a Property
(e.g.) venue.Title, then an update is performed and the venue.Id remains
unchanged. WIhin the ContactData objects, whether I update the value or not
the ContactData.Id is set to 0 by the UpdateModel(...) method, and the
resultant db table contains the old record with the FK set to null and a new
record for the updated values.
In Example 2
The behaviour is similar, although even though the ContactData.Id
Autogenerated value is true the UpdateModel assigned value is
00000000-0000-0000-0000-000000000000 rather than the existing key. Then when
the UpdateModel(..) method is run I get an InvalidOperationException: An
attempt was made to remove a relationship between a ContactLink and a
ContactData. However, one of the relationship's foreign keys
(ContactData.ContactLinkID) cannot be set to null.
This seems to me very strange behaviour which I am really struggling to
understand. I can supply my example 1 as a demo if required, but would
appreciate any pointers on how to solve this.
and a premature question related to editing objects. I thought that I had
managed to sort it out, but unfortunately I haven't.
It may be useful to look at the following post as it was my stating point
and provides a solution which I have implemented:
http://www.microsoft.com/communitie...40bca7c0&lang=en&cr=US&sloc=en-us&m=1&p=1#top
This solution works perfectly for creating records, however when it comes to
updating those records the behavior is strange and I cannot work out why.
I am using Linq2Sql to generate the model returned by a repository pattern
(repo.Get(id)). That Venue repository contains within it's structure the
EntitySet<ContactData> _contactDatas as a variable within a ContactList.
I have two cases both use a database table (ContactData) with an id column
and a fk to the id column of another table (ContactList) thereby giving a
ContactList = 1 to ContactData = many relationship:
Example 1- my demo project
ContactData id column is of type int IDENTITY. The FK to ContactList Allows
Nulls (for example only)
Example 2 - my actual project
ContactData id column is of type uniqueinteger, is a RowGuid and has a
default value = newid(). The FK to ContactList does not allow nulls. Within
Linq2Sql designer AutoGenerated Value = true.
My Controller code to handle a edit post is as follows:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Venue venue = repo.GetVenue(id);
UpdateModel(venue, "Venue", formValues.ToValueProvider());
repo.Save();
Trace.WriteLine(venue.VenueDetail.ContactLink.ContactDatas.ToString());
//However it is not populated with objects
Trace.WriteLine(venue.VenueDetail.ContactLink.ContactDatas[0].ToString());
return RedirectToAction("Edit", new { id = venue.VenueId });
}
For Example 1:
When I load the venue with the model data I have a ContactData object within
the ContactList. If I then step into the UpdateModel(...) method the venue is
updated with data from the formvalues collection. If I update a Property
(e.g.) venue.Title, then an update is performed and the venue.Id remains
unchanged. WIhin the ContactData objects, whether I update the value or not
the ContactData.Id is set to 0 by the UpdateModel(...) method, and the
resultant db table contains the old record with the FK set to null and a new
record for the updated values.
In Example 2
The behaviour is similar, although even though the ContactData.Id
Autogenerated value is true the UpdateModel assigned value is
00000000-0000-0000-0000-000000000000 rather than the existing key. Then when
the UpdateModel(..) method is run I get an InvalidOperationException: An
attempt was made to remove a relationship between a ContactLink and a
ContactData. However, one of the relationship's foreign keys
(ContactData.ContactLinkID) cannot be set to null.
This seems to me very strange behaviour which I am really struggling to
understand. I can supply my example 1 as a demo if required, but would
appreciate any pointers on how to solve this.