I have a mapping table like:
Article_to_Categories
ArticleID CategoryID
How would I insert into this table using linq2sql?
I have a mapping table like:
Article_to_Categories
ArticleID CategoryID
How would I insert into this table using linq2sql?
Provided that corresponding rows exist in the Article and Category tables, this should be no different than a standard insert.
On the other hand, if those tables are empty, then you ll need to insert rows into those tables before you execute SubmitChanges.
Linq-to-Sql will manage the precedence for you.
All in all, I suggest that you simply try to insert rows into the Article_to_Categories table and see what happens.
EDIT: If don t understand how to insert a row in Linq-to-Sql, consider examples on the web:
In my experience, I have had similar problems inserting records in association tables like this one. What works for me is to honor the foreign key relationship and add objects rather than just the foreign key ID.
For example, this didn t work for me:
var newRec = new Article_To_Categories
{
Article_ID = 1,
Category_ID = 2
};
DataContext.Article_To_Categories.InsertOnSubmit(newRec);
DataContext.SubmitChanges();
What worked for me was this:
var newRec = new Article_To_Categories
{
Article = DataContext.Articles.Where(a => a.Article_ID == 1).SingleOrDefault(),
Category = DataContext.Articles.Where(a => a.Category_ID == 2).SingleOrDefault()
};
DataContext.Article_To_Categories.InsertOnSubmit(newRec);
DataContext.SubmitChanges();
As near as I could figure based on my research, if I add the object rather than the ID, the resulting Article_To_Categories object is correctly updated with the contents of the related records. Part of the updating process is the notification that the related records should not be re-inserted. If you just update the new record with the ID s, I don t believe that LINQ-to-SQL will automatically retrieve the records from the Article and Category tables, which means that those related records will be subject to any existing validation, whether from the object or the database.
I need to select rows from database table using filtering by xml-type column. table looks like (short version) id dbfield int xmlfield xml and i m filtering it in this way IQueryable<Data....
I wonder if someone could provide a simple example of the following. (preferably in VB.Net): I have an SQL database with related tables and I am successfully using LINQ to SQL in other areas of my ...
After I do an insert using linq to sql, can I get the Identity_scope value back if my table has an identity column?
I have table A and Table B. Table B contains two columns, Name and ID. Table A contains several columns and a foreign key column pointing to B.ID called B_ID On the client side, I have all the data I ...
Hi I have the following SP, however when I use LINQ to SQL it generates 2 multiple recordsets. For my sanity I am trying to fathom out what it is in the stored procedure that is doing this and would ...
How do I code the Select clause in my LINQ satament to select column aliases so I can sort on them basically I want to accomplish this SQL statement in LINQ: select type_id as id, ...
Is there a way to specify in a data connection or the LinqToSql Designer a schema? Whenever I go to setup a data connection for LinqToSql there doesn t seem to be anyway to specify a schema and I ...
I m trying to transform the SQL Query below into Linq to SQL select Categorias.IdCategoria, Categorias.Nome, SUM(lancamentos.valor) from lancamentos left outer join Categorias on Lancamentos....