-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FromSql and SqlParameter issue when calling by 2 ways the query #6249
Comments
You tried to use Linq to SQL? var emprunteurs3 = from emp in contextDoti.Emprunteur
where ((numeroRcs??"") == "" || emp.NumeroSiren.Contains(numeroRcs)) &&
((numeroPersonne??"") == "" || emp.NumeroPersonne.ToString().Contains(numeroPersonne)) &&
((numeroCompte??"") == "" || emp.Prets.Any(y => y.NumeroCompte.ToString().Contains(numeroCompte))))
select new Emprunteur { property1 = emp.property1, property2 = emp.property2 }; I aways use this and works fine. Otherwise you can use some SqlCommand like this: using(var contextDoti = new YourContextClass())
{
var conn = contextDoti.Database.GetDbConnection();
conn.Open();
var command = (SqlCommand)conn.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "Your query goes here";
command.Parameters.Add(new SqlParameter() { DbType = DbType.TheTypeOfTheParameter, ParameterName = "@ParameterName", Value = valueOfParameter });
// You can use the sintax above for each parameter you have
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = command;
da.Fill(ds);
if(conn.State == ConnectionState.Open)
conn.Close();
} This will return a DataSet with the data. And to transform the DataSet on a Entity you can use the code bellow: List<Emprunteur> items = ds.Tables[0].AsEnumerable().Select(row =>
new Emprunteur
{
PropertyName = row.Field<int>("PropertyName"),
PropertyName2 = row.Field<string>("PropertyName2")
// You can use the sintax above for each property
}).ToList(); Remeber: That is only a way to do what you want. We will apply the correction on the EFCore. |
I think that you didn't understand my problem. The problem is that I query two times the "emprunteurs" BdSet which leads to a duplicate parameters error.
I have the solution to declare an "emprunteurs2" the same way that the "emprunteurs" was and call the count() on the "emprunteur2". Anyway, I think that it should be possible to call two times the FromSql function and the parameters error is just an unintended bug. |
Hi,
Since the SQL LIKE is not already available, with linq queries,I tried to use the FromSql function but it leads to some problems when I want to paginate my results.
Steps to reproduce
The issue
It seems that the query is executed two times with the same parameters which leads to the following error.
Things tried
I tried to use linq to entity with the contains() method instead of SQL LIKE and it is working but it has worse performance and not the wanted behaviour.
This makes me think that the problem is related to the FromSql function.
Here is the code.
Further technical details
EF Core version: 1.0.0
Operating system: Win 7 X64
Visual Studio version: VS2015 update 3
The text was updated successfully, but these errors were encountered: