I started receiving the error the full-text query parameter for fulltext query string is not valid after upgrading to .NET 4 when calling a function in SQL Server 2008 from Linq to SQL. It seems LTS is sending the string as nvarchar(4000) event though I had defined the paramater to the function with a length of nvarchar(200).
The work around that I put in place as we define the parameter to my function as 4000. Not ideal, but it resolved the error.
Often I find the need to dynamically specify the order by for a LINQ to SQL query. I came across a very nice example of this here.
I needed to use this technique with a CSLA class, so I needed to call it in a sligthly different manner than the way explained in the article. I was grouping information and needed to then dynamically set the order by and finally place the results into a CSLA read only list.
I accomplished this as follows:
- First I specified my LINQ query and selected the results into an annonymous type and placed into a variable (var data = ...)
- Next, called my OrderBy extension method on the LINQ query created in #1
var ordered = data.OrderBy(criteria.OrderByProperty, criteria.OrderByDesc);
The criteria object is the typical pattern used in CSLA to pass data to your data access methods such as fetch.
- Finally, I selected the data elements into my CSLA list item, and add to the parent list object
var selected = ordered.Select(a => MyCslaListItem.GetMyCslaListItem(a.Id, a.Name, a.Email, ...));
this.AddRange(selected);
I understand I could do this in fewer steps, but am leaving as it to explicitly outline the approach taken.
Recently I ran into an issue with the contains method in LINQ in conjunction with the SingleOrDefault method.
My query was similar to:
var data = (from i in ctx.DataContext.orders
where order.Contains(i.Name)
select i).SingleOrDefault();
and this was throwing the error:
System.InvalidOperationException: Sequence contains more than one element
Turns out the Contains translated to a LIKE '%...%' and was returning multiple records. By changing this to i.Name == order, then I got back the single result I was expecting.
Recently I was working on a custom page in a Dynamic Data project and the "Delete" link was not working on a GridView that had a LinqDataSource as its data source. Below are the steps I followed to resolve:
First, I was getting an error when clicking delete for a record in the GridView
LinqDataSource
'GridDataSource' does not support the Delete operation unless
EnableDelete is true.' when calling method
This was fixed easily by setting EnableDelete="true" on the LinqDataSource.
Next up, I needed to set the DataKeyNames on the GridView. This basically indicates what the where condition will be for the delete operation. I set the GridView DataKeyNames equal to the name of the primary key column in my table.
That will probabaly resolve for most people, but for me, it did not. I had also set the Select property on the LinqDataSource and this was preventing the delete from occuring. Once I REMOVED the value for this propery, all worked.
The below article was helpful in diagnosing:
http://msdn.microsoft.com/en-us/library/bb514963.aspx
When using the LINQ to SQL designer surface in Visual Studio, each time
you add an object (table, procedure, etc), you will end up adding
connection string information to your config file. This can be
confusing as the names used for the connection string may be different
then what you want to use and you may end up with multiple connection
strings, especially if you have multiple developers working on the same
project.
To address this issue, you can:
- Create a partial class for your data context and create a parameter-less constructor as follows
public partial class LinqAppDataContext
{
public LinqAppDataContext()
: base(ConfigurationManager.ConnectionStrings["AppConnectionString"].ToString())
{
}
}
In this example, I already have a LinqAppDataContext class, and I am
creating a partial class that calls the constructor on the base class
that takes a connection string. I then pull this connection string
from the config file.
- Step 1 is a one time setup. The remaining steps need to be followed each time you add a new object to the designer surface
- Add a new object to the designer surface (for example, drag a table from the Server Explorer)
- Click on an empty section of the designer surface (dbml file)
- In
the properties window, go to Connection, then clear the information
from the Connection String property and then set the Application
Settings property to false. By doing this, the parameter-less
constructor from step 1 above will kick in and pull the connection
string from your config file.
- Verify that no extra connection strings have been added to your projects settings or config files
Hopefully this will help you in clearly knowing which connection string will be used with your LINQ to SQL context classes.