Looks like you are getting some entities with your select new
EditableCallout, and those are not the entities you have in the database. If you want to insert or update you should do that on the entities that you have in the db and not on your "own" classes that is a combination of values from the database entities
* REMEMBER TO MARK THE ANSWER TO YOUR QUESTION * .NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
Looks like you are getting some entities with your select new
EditableCallout, and those are not the entities you have in the database. If you want to insert or update you should do that on the entities that you have in the db and not on your "own" classes that is a combination of values from the database entities
He is doing that, he's getting a database entity then manually mapping data from a dto to that entity.
Looks like you are getting some entities with your select new
EditableCallout, and those are not the entities you have in the database. If you want to insert or update you should do that on the entities that you have in the db and not on your "own" classes that is a combination of values from the database entities
He is doing that, he's getting a database entity then manually mapping data from a dto to that entity.
public void Update(EditableCallout callout)
{
IQueryable<CalloutValue> da = (
from c in wdc.CalloutToProducts
join cv in wdc.CalloutValues
on c.CalloutID equals cv.CalloutID
where c.id == callout.id
&& cv.CalloutID == callout.CalloutID
select cv
);
da.First<CalloutValue>().Value = callout.Value;
wdc.SubmitChanges();
}
Thanks everyone for the help.
Marked as answer by drdexter33 on Apr 19, 2012 12:43 PM
drdexter33
Contributor
2289 Points
889 Posts
Update DB using System.Linq.DataContext (LINQ To SQl)
Apr 16, 2012 03:19 PM|LINK
I'm using a Linq to SQL class as a data context to retreive and update tables in a database.
The retreive is working ok, however the Update is not.
There are no exceptions being thrown, but the data isn't being updated and I can't figure out what's preventing that.
Here's my Repository:
namespace MarketingWebsiteTools.Models { using System; using System.Collections.Generic; using System.Linq; using System.Web; using MarketingWebsiteTools.ViewModels; using MarketingWebsiteTools.Models; public class SessionCalloutRepository { WebsiteDataContext wdc; public SessionCalloutRepository() { wdc = new WebsiteDataContext(); } //WebsiteDataContext wdc = new WebsiteDataContext(); public IList<EditableCallout> All() { //WebsiteDataContext wdc = new WebsiteDataContext(); IList<EditableCallout> result = (IList<EditableCallout>)HttpContext.Current.Session["Callouts"]; if (result == null) { HttpContext.Current.Session["Callouts"] = result = (from c in wdc.CalloutToProducts join cv in wdc.CalloutValues on c.CalloutID equals cv.CalloutID select new EditableCallout { id = c.id, ProductIdentifier = c.ProductIdentifier, DateStart = c.DateStart, DateEnd = c.DateEnd, Value = cv.Value, IsActive = c.IsActive }).ToList(); } return result; } public EditableCallout One(Func<EditableCallout, bool> predicate) { return All().Where(predicate).FirstOrDefault(); } public void Insert(EditableCallout callout) { callout.id = All().OrderByDescending(c => c.id).First().id+ 1; All().Insert(0, callout); } //-->UPDATE: public void Update(EditableCallout callout) { EditableCallout target = One(c => c.id == callout.id); if (target != null) { target.ProductIdentifier = callout.ProductIdentifier; target.DateEnd = callout.DateEnd; target.DateStart = callout.DateStart; target.Value = callout.Value; target.IsActive = callout.IsActive; wdc.SubmitChanges(); } } } }My first guess was that maybe the class didn't have a PK on it, so I added that and check the IsReadOnly property which is set to false.
A friend said that's there's an IsDirty property on the DC, however I couldn't find that..
Thanks..
Knecke
Contributor
3712 Points
838 Posts
Re: Update DB using System.Linq.DataContext (LINQ To SQl)
Apr 16, 2012 03:23 PM|LINK
Looks like you are getting some entities with your select new EditableCallout, and those are not the entities you have in the database. If you want to insert or update you should do that on the entities that you have in the db and not on your "own" classes that is a combination of values from the database entities
.NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Update DB using System.Linq.DataContext (LINQ To SQl)
Apr 16, 2012 04:13 PM|LINK
He is doing that, he's getting a database entity then manually mapping data from a dto to that entity.
EditableCallout target = One(c => c.id == callout.id); if (target != null) { target.ProductIdentifier = callout.ProductIdentifier; target.DateEnd = callout.DateEnd; target.DateStart = callout.DateStart; target.Value = callout.Value; target.IsActive = callout.IsActive; wdc.SubmitChanges(); }@drdexter33 - Does your table have a primary key?
Blog | Twitter : @Hattan
Knecke
Contributor
3712 Points
838 Posts
Re: Update DB using System.Linq.DataContext (LINQ To SQl)
Apr 16, 2012 04:14 PM|LINK
No, One() is calling All() which in turn creates a dto
.NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
drdexter33
Contributor
2289 Points
889 Posts
Re: Update DB using System.Linq.DataContext (LINQ To SQl)
Apr 16, 2012 04:50 PM|LINK
Yes, It did not originally, however, I added one.
Knecke
Contributor
3712 Points
838 Posts
Re: Update DB using System.Linq.DataContext (LINQ To SQl)
Apr 17, 2012 05:31 AM|LINK
You cannot update your EditableCallout instance since its not part of the context. understand?
.NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
Knecke
Contributor
3712 Points
838 Posts
Re: Update DB using System.Linq.DataContext (LINQ To SQl)
Apr 19, 2012 06:21 AM|LINK
Did you solve this issue?
.NET Developer (ASP.NET, MVC, WPF) MCTS .NET 4 (Web, WCF)
Blog | Twitter
drdexter33
Contributor
2289 Points
889 Posts
Re: Update DB using System.Linq.DataContext (LINQ To SQl)
Apr 19, 2012 12:43 PM|LINK
Yes.
Here's the updated code:
public void Update(EditableCallout callout) { IQueryable<CalloutValue> da = ( from c in wdc.CalloutToProducts join cv in wdc.CalloutValues on c.CalloutID equals cv.CalloutID where c.id == callout.id && cv.CalloutID == callout.CalloutID select cv ); da.First<CalloutValue>().Value = callout.Value; wdc.SubmitChanges(); }Thanks everyone for the help.