I am building an ASP.NET 3.5 web application using C# and Linq to SQL. This is my first attempt at using Linq to SQL so I have a few questions.
This application is pretty much data driven as opposed to domain driven (not my choice). In this application have have the following tables:
VendorMaster
VendorAddresses
Addresses
The VendorAddresses table has two Foreign Keys
VendorMaster
Addresses
I was working on creating a new VendorAddress record and I ran into all sorts of problems on my InsertOnSubmit/SubmitChanges. At first I thought that I could just set the VendorMaster ID and the Addresses ID properties on the VendorAddresses record, but
that didn't work as Linq to Sql threw an error. It seems that the only way that I could get around the problems that I was having was to either read or attach the VendorMaster and Addresses record to the VendorAddresses record.
My questions are as follows:
Can anyone explain why this has to be done as opposed to setting the foreign key id's like VendorMaster ID and Address ID on the VendorAddresses entity?
If I decided I wanted to create a new VendorMaster and a new VendorAddress address record would the InsertOnSubmit to the VendorMaster record be enough if the VendorMaster entity had an attached VendorAddress or would I still need to do a seperate InsertOnSubmit
for the VendorAddress? In other words is Linq to Sql smart enough to look at the entity and apply the proper changes?
If there a good book or blog that describes how to handle foreign key relationships using Linq to SQL?
Thanks
ps. If it makes any difference, I move the data from the entity classes into DTO classes when I read the data and move the data from DTO classes to the entity classes when I write the data.
categoryAttribute ct = new categoryAttribute();
ct.category = new category();
ct.category.blablabla = "blablabla";
ct.blablabla= "blablabla';
DatacontextObj.categoryAttributes.InsertOnsubmit(ct);
DatacontextObj.SubmitChanges();
I would like to thank you for the quick response. While I do understand what you posted, it would help me understand why as to opposed to how to handle foreign keys if you could answer the questions I posed. For instance, why can't I just set the ID fields
on my VendorAddresses table, or if doing one insertonsubmit to my VendorMaster entity also save changes to the attached VendorAddress?
NewToDotNet
Participant
1052 Points
734 Posts
Linq to Sql - How to handle Foreign Keys
Aug 09, 2010 01:17 PM|LINK
I am building an ASP.NET 3.5 web application using C# and Linq to SQL. This is my first attempt at using Linq to SQL so I have a few questions.
This application is pretty much data driven as opposed to domain driven (not my choice). In this application have have the following tables:
The VendorAddresses table has two Foreign Keys
I was working on creating a new VendorAddress record and I ran into all sorts of problems on my InsertOnSubmit/SubmitChanges. At first I thought that I could just set the VendorMaster ID and the Addresses ID properties on the VendorAddresses record, but that didn't work as Linq to Sql threw an error. It seems that the only way that I could get around the problems that I was having was to either read or attach the VendorMaster and Addresses record to the VendorAddresses record.
My questions are as follows:
Thanks
ps. If it makes any difference, I move the data from the entity classes into DTO classes when I read the data and move the data from DTO classes to the entity classes when I write the data.
Ahmed.Mostaf...
Member
309 Points
54 Posts
Re: Linq to Sql - How to handle Foreign Keys
Aug 09, 2010 01:32 PM|LINK
check the post below
http://forums.asp.net/t/1577799.aspx
NewToDotNet
Participant
1052 Points
734 Posts
Re: Linq to Sql - How to handle Foreign Keys
Aug 09, 2010 01:51 PM|LINK
I would like to thank you for the quick response. While I do understand what you posted, it would help me understand why as to opposed to how to handle foreign keys if you could answer the questions I posed. For instance, why can't I just set the ID fields on my VendorAddresses table, or if doing one insertonsubmit to my VendorMaster entity also save changes to the attached VendorAddress?
Thanks