Table not getting updated while using LinQ - vb.net

I am trying to update a table using LinQ. Though records are getting inserted, for some reason they are not getting updated.
what can be possible problem
Dim db as new empDataContext
Dim emptable as new employee
if update then
emptable=GetEmp(txtempID.Text)
emptable.Name="Test"
emptable.Age=11
emptable.City="NYC"
else
emptable.Name="Test"
emptable.Age=11
emptable.City="NYC"
emtable.deptID=10
db.employee.InsertOnSubmit(emptable)
end if
db.SubmitChanges()

Judging just from what I can see here, I'm guessing your GetEmp method is using a different data context to retreive the data than the one you're using to save it back to the DB.
When using LINQ to SQL, the context is what tracks the changes to the tables. If you're not careful and mix Contexts by accident, you can get strange behaviors like this.
You can test by chaging:
emptable=GetEmp(txtempID.Text)
to
// Returns the first matching employee with the id
emptable = (from e in db.Employees
where e.id == txtempid.Text).FirstOrDefault()
If you find that the context is the issue, just modify your GetEmp method to accept the context as a parameter rather than creating a new one itself.

What does GetEmp do? In particular, as presented it appears that it does not have a reference to the empDataContext named db. DataContexts are examples of identity maps and as such they track items that have been loaded from a persistence mechanism. If you are using a different DataContext in GetEmp then the DataContext db does not know about the instance of employee with SomeID equal to the value represented by txtempID.Text.
So either pass a reference to db into GetEmp or change your code to the following:
emptable = db.Single(Function(e as employee) e.SomeID=Int32.Parse(txtempID.Text))
then your update should work.

If I had to guess, I would say that the GetEmp() call is not using the same database context object. Therefore, Linq-To-SQL doesn't think any changes are occuring in the "db" database context.
Randy

Related

Django: How do I update only ONE record in my database?

I'm having a lot of trouble trying to update a single (record) object in my database.
context['eval_list'] = Evaluering.objects.update(eval_relationer_id=self.kwargs.get('pk'))
I use objects.update, but it updates ALL my objects fk. How do I achieve only updating one object? I have also tried this:
context['eval_list'] = Evaluering.objects.update_or_create(eval_relationer_id=self.kwargs.get('pk'))
But this creates a new object and does not update the record that I want to update. I know why it creates a new objects, and it is because the FK I'm trying to update is null. Surely, there must be a way to only update and not create a single record? What am I missing here?
I tried adding a filter, but it feels redundant? I tried this:
context['eval_list'] = Evaluering.objects.filter(eval_relationer_id=self.kwargs.get('pk')).update(eval_relationer_id=self.kwargs.get('pk'))
I did consider trying to create an ID of the FK instantly and not later on, but I couldn't really get that to work, but if I created an ID then the update_or_create would work because an ID would exist already, BUT I cannot believe that I can't update a single object without create?
If creating the ID earlier on is the only work around, I will have to figure out how.
MyModel.objects.filter(pk=some_value).update(field1='some value')
The filter gets your object (returns the Queryset with only that object), then the update changes some other field that is not the PK to whatever you want.
In your case probably something like this:
context['eval_list'] = Evaluering.objects.filter(eval_relationer_id=self.kwargs.get('pk')).update(some_attribute='some value')
After help from #Hanny I've figured out was going wrong.
I was trying to filter by the eval_relationer_id, when I should have been filtering by the evaluation pk and getting that specific PK. Otherwise I would be updating ALL the values which is not what I wanted.
So by filtering by pk:
filter(pk=self.kwargs.get('pk'))
And updating by the attribute / fk that I want to update
update(eval_relationer_id=self.kwargs.get('pk'))
This is the end-result:
context['eval_list'] = Evaluering.objects.filter(pk=self.kwargs.get('pk')).update(eval_relationer_id=self.kwargs.get('pk'))

Simple.Data Eager Load SimpleQuery object

It appears that Simple.Data will lazy load by default.
I simply want Simple.Data to query the database and put the results in an object. For example, as soon as this piece of code is executed the results from the database should be stored in employeeData and the database should not be called again:
var employeeData = db.Employee.FindAllByEmployeeId(employeeId) .Where(db.Employee.EmployeeId == 1);
How do I do this? The Simple.Data documentation only describes how to eager load joins. I do not require any joins, simply to get results from a table when I choose to. If I include this WithEmployee() it will do a left join on the Employee table and output the same data twice...
Figured it out... turns out I can just create a list from the output which will save the data
eg.
.ToList<Employee>()

SubmitChanges with LINQ to SQL and User Defined Functions

On the SQL server I create a UDF. Let's name it fnCompanyDetails. It selects some information about a company from several joint tables.
I then drag-and-drop this function in the new .dbml of my VB project. I want to use it with LINQ. Smth like this:
Dim company = (From c In d.fnCompanyDetails(sap)
Select New With {
.Sap = c.txtSAP,
.CompanyName1 = c.txtCompanyName1, _
.CompanyName2 = c.txtCompanyName2, _
})
The result of this query I display to the user in a form. If some field is changed I want to send the changes to my database, but is this possible if I'm quering like this, from a UDF?
Thank y
No, it is unfortunately not possible to do in a simple way. Linq-to-sql supports reading to custom types, but only support updates through the entity types that exactly corresponds to a table.
Normally the best way is to always read pure entity objects if they are read with the intention to update them.
Another solution is to create entity objects out of the data returned from the udf and then attach those entities to the context. If you attach the entities in their original state first and then make changes after attaching, you should get away without any change tracking problems.

NHibernate Hanging on Lazy Loaded Query

First of all, I'm using Fluent NHibernate with LinqToNHibernate.
I've got a query to do a search on a table based on what data the user entered. So, for example, I'm doing something like this:
'build the initial query that we will filter--this is lazy loaded
Dim results As IEnumerable(Of Customers) = Me.GetCustomers()
'filter by owner name
If String.IsNullOrEmpty(OwnerName) = False Then
results = results.Where(Function(s) s.Name.Contains(OwnerName))
End If
'execute query
results = results.ToList()
So essentially I'm building a where statement on the sql statement if the user wants to search by name. I'm using all lazy configurations in my mappings, so the query shouldn't be retrieving the items until the "ToList()" is called. The problem is that NHibernate hangs on this statement. I thought at first it was because there were so many records in the db(about 500,000).
But I changed the line where I am filtering the results to this:
results = SessionFactoryProvider.SessionFactory.CurrentSession.Linq(Of Customer).Where(Function(c) c.Name.Contains(OwnerName))
And it works very quickly. But I can't seem to figure out why. Any ideas?
In the first case you are retrieving all the records and using Linq-to-objects to filter that list.
In the second case, you are sending the query to the database (NHibernate.Linq converts your expression into a SQL WHERE clause).
Now, I don't know what is the return type of GetCustomers, but since you're storing it into an IEnumerable(Of Customer), .NET has no way of knowing about an underlying provider. If GetCustomers' code is something like:
Return CurrentSession.Linq(Of Customer)
...Then the problem is with that conversion. Just change the first line to:
Dim results As IQueryable(Of Customers) = Me.GetCustomers()

In a datacontext are inserted values not available within the datacontext until after submitchanges?

I'm going through an XML file of articles and the journalist(s) that wrote them. As we are adding the articles into _Data our datacontext we may come across a journalist that needs adding so we do this:
newJourno = New journalist With {.name = strJournalist}
_Data.journalists.InsertOnSubmit(newJourno)
.articles_journalists.Add(New articles_journalist With {.id_journalist = newJourno.id, .id_article = .id})
However subsequently we may come across this same journalist again and nothing is returned when we do this:
Dim journo = _Data.journalists.Where(Function(s) s.name = strJournalist).SingleOrDefault
So it uses the code above again to insert the same journalist again.
Once all of our inserts are done we do a submitchanges. At this point it has a head fit:
INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_articles_journalists_journalists'. The conflict occurred in database 'blah', table 'journalists', column 'id'. The statement has been terminated.
From looking through the sql generated in sql profiler you can see that it is trying to add some journalists more than once, this will fail as the name must be distinct. The subsequent records that are trying to be inserted with these journalists are failing as the journalist wasn't updated.
Surely if I have a collection of journalists, add some to it and then look in my collection I should see all of them and not just the original ones. I can fudge it I guess by doing a submitchanges but that seems a bit silly.
Thanks in advance,
Dave.
If you want to add two child-parent rows to the database, you must assign the entity, instead of the Id column, the Id will be autogenerated and will be available only after the submit changes.
You have to do a articles_journalist object, and then assign the newJourno entity to this:
articles_journalist.journalist = newJourno;
CMS is right about needing to assign the object, not the id.
However this doesn't seem to get around the problem of the datacontext not realising that it has had new stuff added to it until you submitchanges. I can only presume this is by design and therefore I am now calling submitchanges as and when the code inserts objects that we later search for.
"the name must be distinct."
This is a serious design flaw. Person names are never unique.