LINQ to SQL Classes & Datacontext are not created - asp.net

Afternoon all
This is driving me nuts.
For no apparent reason (of course there must be one), my web project will no longer generate LINQ to SQL classes/data contexts!!!
I am going about the usual routine of right clicking the project, adding new item, selected LINQ to SQL classes, then dragging over a table from Server Explorer, saving and build but no...the bloody thing won't appear!!!
Does anyone know why this might be happening/what have I done wrong?

Figured it out.
Basically, NEVER rename any items.
I had previously been using OnlineReporting in one item, the name space when linq creates the classes was using this as it's namespace.
However, I then later changed this to Reporting but when the linq to sql generation occurred, it was still using the 'old' namespace.
I went into the designer.cs file, amended the name space to match the new format and it worked.

Related

Entity Framework problem in new Entity Inheritance

Could someone with some good knowledge answer this question please. Im having a problem with the schema in my Students.aspx page. I have done the walkthrough, but I get an error and I have copied the edmx file from the sample into my application and I still get the error. This is the error:
'EnrollmentDate' is not a member of
type 'SchoolModel.Person' in the
currently loaded schemas. Near simple
identifier, line 6, column 4.
I have also created a new EntityDataSource and I have the same problem. The sample works fine but I can't seem to get the EnrollmentDate and HireDate fields to be part of the 'SchoolModel.Person' Entity. The part which is Upto setting the EntityTypeFilter then I run the app and I get problems
It sounds like you're following the example on MSDN. By the end of the example, the fields EnrollmentDate and HireDate are no longer on the SchoolModel.Person class, they're on the subclasses SchoolModel.Instructor and SchoolModel.Student.
I faced the same problem while going through that walkthrough. The solution: Just delete the old EntityDataSource control on the page, put new EntityDataSource, configure it as given in walkthrough (with entity set name: People; EntityTypeFilter: Student or instructor), And every thing works as it should!!
Faraz
It sounds like you may have missed one or more EDS controls when you added the EntityTypeFilter attributes. Are you sure all your EDS controls that access students or instructors have EntityTypeFilter="Student" or EntityTypeFilter="Instructor"?

ASP.NET DataView - problem with RowFilter and application cache

Good afternoon ladies and gents --
I've been tasked with finding and fixing a bug in an unfamiliar legacy application that had some recent changes made to it, but I don't have an easy way (that I know of) to test my theory. I'm hoping your collective knowledge will verify the test for me.
This application lazy loads lookup lists (tongue-twister?) into DataTables from a database and stores them as an object in HttpContext.Current.Application (an HttpApplicationState).
Before the changes were made, one of the lookup tables was bound to a DropDownList in the following manner (contrived):
Me._lookupList = TheSession.LookupCache.SomeLookupListName.DefaultView
...
ddl.DataSource = Me._lookupList
where 'SomeLookupListName' is a read-only property that returns a DataTable from HttpContext.Current.Application. The changes added some code that filters the private Me._lookupList (DataView) before being bound to the DropDownList:
Me._lookupList.RowFilter = "SomeTableIDColumn <> " & ...
What's happening, if you haven't guessed it already, is that that DataView is now filtered for every user of the application. I looked around the code and found that most other lookup lists are copied to local members in this fashion:
Me._lookupList = New DataView(TheSession.LookupCache.SomeLookupListName)
Since I don't know how to attack my local debug session pretending to be multiple users, will changing the code to use the latter method actually be any different than the former? Does filtering the result of DataTable.DefaultView actually apply the filter to the underlying DataTable differently than if wrapping the table with a New DataView(...)?
Would it make sense to simply clear the row filter after the DropDownList is bound (seems like a bad solution)? I'd like to stick to the ugly conventions this application uses so that I don't surprise another developer down the road who gets a similar task, otherwise I'd just bypass the application state and grab the items right out of the data repository.
I appreciate your feedback.
Does filtering the result of
DataTable.DefaultView actually apply
the filter to the underlying DataTable
differently than if wrapping the table
with a New DataView(...)?
Yes. It creates a new view which the filter is applied to. The filter is not applied to the table directly. Following the pattern to use the new view will work.
BTW, easy to test multiple sessions against your debugger. Just open up two different browsers (IE and FF) and point to the same app. User login might be the same, but the sessions will be unique.

ASP.NET MVC- Bizarre problem - suddennly lost all LINQTO SQL data context objects

I was making an edit to a long existing project. Specifically I added some fields to a table and had to delete the table from the LINQTOSQL designer and re-add it. doesn't Also had to do the same for a view. Mode some other code changes and went to build . Now my project won't build because it can't resolve any of the data context objects (all tables and views) in my code. I don't know what I did or how this happeened. I have many tables and views in the project's L2S data context so I don't wont to try and do over. Please any suggestions on how to resolve this problem are greatly appreciated. Desparate! The error messages I am getting are the familiar
The type or namespace name 'equipment' could not be found (are you missing a using directive or an assembly reference?)
Verify that your "entities.desinger.cs" class was auto generated if not go to the .dbml file and right click on run custom tool to generate it.

LinqtoSQL and problems

I am using link to sql, I have a connected set of objects.
I start out and do a linq statement like this
Dim L= from II in context.InventoryItems select II
Dim L2 = L.tolist
The second line was so that I could narrow down where the problem was occuring. When the second line is hit I get an error "The EntitySet is already loaded and the source cannot be changed"
Any ideas what might be causing this?
Omer's comment brings up a very good point: is this DataContext being re-used from a previous operation? If so, you may want to check out Dino Esposito's blog post regarding lifetime of the DataContext to make sure you're not keeping it around too long.
That error sounds like maybe you have already loaded data from the InventoryItems table using that DataContext and possibly made some changes to entities bound to the DataContext that you have not yet submitted. If you try your code with a brand new DataContext without specifying any special DataLoadOptions it should work.
For anyone interested, be careful what you do in the constructor, I was initializing some stuff in the constructor that I shouldn't have been and it caused an error on loading from the datacontext.

Gridview sorting challenge when moving from Winforms to ASP.NET 2.0 Webforms

I have a problem with Gridview sorting that is similar to others but I'm binding to a collection object as opposed to a data table.
The existing business rules and data access layers of an application follow the pattern of having an object and, if you need a collection of objects of that type, to have another class inheriting CollectionBase and implementing IBindingList.
For desktop applications, it was easy to databind a gridview to one of these objects and there weren't any problems with turning on column sorting. Everything was 'in state' in the desktop app's presentation layer.
Now that code is being moved to a new web application (ASP.NET 2.0, VB codebehind pages).
I've played around with what I had to do to only have certain columns of the collection show up in the gridview and the gridview looked pretty good. When I turned on 'allow sorting', that's when the problems showed up.
I'm getting the error about not having a .Sorting method, etc. In researching this, I found all sorts of solutions that were easily implemented with dataviews if my source was a data table. But it's not - it's a collection. I tried to "cheap shot" a datasource by converting the collection to an XML memory stream and them trying to .ReadXML back into a dataset but that didn't work [Root element is missing error was as far as I got in the dataset.ReadXml(ioTemp) where ioTemp was the System.IO.MemoryStream used in the xml serializer].
Because of the old desktop apps, I've never had to worry about sorting a collection since the gridview handled it once it was loaded. In fact, it's a 'standard' that the collection's .SortProperty, .SortDirection and .ApplySort all through NotSupportedExceptions (I inherited this code from programmers long gone).
Is there an easy way to convert the collection to a data table or a way to sort the collection without having to go back to the database each time? Object Data Sources won't work becuase of the intricate rules in how the objects are built - the wizards in VS2005 just can't handle what we need to do (grabbing data from several tables conditionally to make an object).
Thanks in advance.
Have you considered client side sorting instead?
I have used the jquery tablesorter plugin in the past with ASP Gridviews.
http://tablesorter.com/
I had a similar issue and i needed to implement IComparable on the objects. Basically to sort a collection of objects you need a way to distinguish their order. The IComparable interface has one method called Compare which allows the .Net framework to work out the order of the objects when you sort them. You need to implement this method yourself to get the sort method to work.
Google results
You don't mention the error message so i cant be sure if this is the case, can you post the error?
EDIT :
In regards to your comment; you can implement multi column sorting, it just requires more work. You can specify the fields to sort the collection by and then use this information within the CompareTo Method.
Have a look at this
Given that you apparently are populating the grid with a collection of your own objects, this sounds like a perfect job for Linq for Objects. With just a little elbow grease you can achieve what is effectively an SQL Select statement against your collection. Very cool stuff.
http://www.hookedonlinq.com/LINQtoObjects5MinuteOverview.ashx
Also, do you really just want to sort the data in the grid? If so, then'd definitely pursue using Linq against your objects. However, rarely does sorting the contents of the grid really answer the problem ("sorting the grid" usually translates into changing the access path of the data used to fill the grid.) Browser apps aren't like Windows apps and don't have a full-time connection to the underlying data source to make things happen quite as magically as the DataGridView in Windows makes things seem.
You can put link buttons with an On_Click event as the header's of each column.
When the event is triggered, figure out which header was clicked on (one method per header or a commandArgument value). Once that is know, do a .orderBy or .OrderByDescending by on the collection of objects, and put the result back in as datasource of the gridview and databind on that.
In the year since I originally asked this question, I managed to get a new 'standard' implemented so that collections of business objects were now generic lists.
So now a "Collection class" that is little more than a "Inherits List(Of MyBusinessObject)" with a Sort Method that looks like this (performance wasn't an issue):
Public Overloads Sub Sort(ByVal strPropertyName As String, ByVal strDirection As String)
Dim arSortedList As New ArrayList
For Each item As MyBusinessObject In Me
arSortedList.Add(item)
Next
arSortedList.Sort(New CaseInsensitiveComparer(Of MyBusinessObject)(strPropertyName, strDirection))
For intI As Integer = 0 To arSortedList.Count - 1
Item(intI) = arSortedList(intI)
Next
End Sub
This seemed to work perfectly with the methodology used by the GridView for firing events.

Resources