Dim BoozeContext As BoozeEntities = New BoozeEntities()
Dim recipes As New ObjectQuery(Of Recipe)(
"SELECT VALUE recipe FROM BoozeEntities.Recipes AS Recipe " &
"WHERE recipe.RecipeName = 'Hot Stuff!!'", BoozeContext)
Array.ForEach(Of Recipe)(
recipes.ToArray(),
Sub(x)
Console.WriteLine(
x.RecipeName & " - " & x.RecipeText
)
Array.ForEach(Of Bottle)(
x.Bottles.ToArray(),
Sub(y)
Console.WriteLine(
y.BoozeFriendlyName
)
End Sub)
End Sub
)
When that didn't work I followed the guidelines in the explanation in the MSDN ObjectQuery(Of T) Class entry. This was
Using context As New AdventureWorksEntities()
' Call the constructor with a query for products and the ObjectContext.
Dim productQuery1 As New ObjectQuery(Of Product)("Products", context)
For Each result As Product In productQuery1
Console.WriteLine("Product Name: {0}", result.Name)
Next
Dim queryString As String = "SELECT VALUE product FROM AdventureWorksEntities.Products AS product"
' Call the constructor with the specified query and the ObjectContext.
Dim productQuery2 As New ObjectQuery(Of Product)(queryString, context)
For Each result As Product In productQuery2
Console.WriteLine("Product Name: {0}", result.Name)
Next
' Call the constructor with the specified query, the ObjectContext,
' and the NoTracking merge option.
Dim productQuery3 As New ObjectQuery(Of Product)(queryString, context, MergeOption.NoTracking)
For Each result As Product In productQuery3
Console.WriteLine("Product Name: {0}", result.Name)
Next
End Using
But this did not work I got an error which was the equivalent of
"Value of type 'AdventureWorksEntites' cannot be converted to 'System.Data.Objects.ObjectContext'. "
I am therefore confused. Why isn't this working for me?
(p.s. This forum is not emailing the notice of replies to my provided email address. If I don't reply right away, that is why.)
Just convert the Context down 1 level then you can use ObjectQuery. Similiar following.
((IObjectContextAdapter)context).ObjectContext.
ObjectStateManager.
ChangeRelationshipState(course, instructor, c => c.Instructor, EntityState.Added);
Note, that if you are updating (not just adding) a relationship, you must delete the old relationship after adding the new one:
((IObjectContextAdapter)context).ObjectContext.
ObjectStateManager.
ChangeRelationshipState(course, oldInstructor, c => c.Instructor, EntityState.Deleted);
I see your cast but am not exactly sure how it would be used in this case. (E.G. would you case the context upon creation or in the objectquery statement? Particularly as neither of the examples felt the need to cast the context which they actually created
as the entities object.)
Can you provide an example that actually uses ObjectQuery and an entities object? (An example in VB would be nice but not absolutely necessary.)
Maybe I need someone with some VB skills to answer this.
I see no logical reason why you are creating a separate class member to carry out what is supposed to be a simple LINQ query.
None of the LINQ examples advocate or suggest that.
If LINQ requires all of that then what possible advantage would it serve to use it? I could have finished this twenty times over using straight ADO.net SQLClient objects with standard SQL queries. But I was trying to learn the "new improved" method of
data access. If I have to do all of this casting and class member creating just to do a simple Select query, then there is no benefit to the Entity Framework, and Microsoft would be guilty of trying to foist another "Vista" on the development community.
However, I refuse to believe it is all that hard and that all the examples given are wrong. I must be missing something in those examples but I don't know what.
And the first example source provides a further example:
Dim rn As String = "Hot Stuff!!"
Dim recipe As Recipe =
BoozeContext.Recipes.Where("it.RecipeName = @rn",
New ObjectParameter("rn", rn)).Execute(MergeOption.AppendOnly).First()
For Each bottle As Bottle In recipe.Bottles
Console.WriteLine(bottle.BoozeFriendlyName)
Next
But this throws an error reporting:
"Overload resolution failed because no accessible 'Where' accepts this number of arguments."
Has this framework been completely redone from scratch since these examples were written? (But the one from MSDN says it is good for VS 2010).
So you saying the Entity Framework has been changed so that the type of object returned by the call to instantiate a new object of type YadaYadaEntities no longer returns a object compatible with ObjectContext but only with dbContext which then has to be
CTyped in order to use it with ObjectQuery.
The change therefore invalidates the example given in MSDN at
http://msdn.microsoft.com/en-us/library/bb345303 because it is still shows the variable "context" returned by the instantiation of the entities as being compatibile with an QueryQuery.
And the tutorial given on the other page has also been invalidated because of this change. So i can no longer trust anything written online unless it specifies it is for Entity Framework 5.
So what other breaking changes were made in this upgrade to Entity Framework?
Anyway this issue got to be too much work to be doing merely to dispaly a proof of concept site to the client. I just screw it and resorted to using a SQLDataReader.
joeller
Member
171 Points
168 Posts
Attempt to use ObjectQuery does not work
Jan 22, 2013 08:44 PM|LINK
I am trying to build an application using Entity Framework. I was following example code on page http://visualbasic.about.com/od/usingvbnet/a/EntityFmwk.htm . It used formula for generating an ObjectQuery,
Dim BoozeContext As BoozeEntities = New BoozeEntities() Dim recipes As New ObjectQuery(Of Recipe)( "SELECT VALUE recipe FROM BoozeEntities.Recipes AS Recipe " & "WHERE recipe.RecipeName = 'Hot Stuff!!'", BoozeContext) Array.ForEach(Of Recipe)( recipes.ToArray(), Sub(x) Console.WriteLine( x.RecipeName & " - " & x.RecipeText ) Array.ForEach(Of Bottle)( x.Bottles.ToArray(), Sub(y) Console.WriteLine( y.BoozeFriendlyName ) End Sub) End Sub )When that didn't work I followed the guidelines in the explanation in the MSDN ObjectQuery(Of T) Class entry. This was
Using context As New AdventureWorksEntities() ' Call the constructor with a query for products and the ObjectContext. Dim productQuery1 As New ObjectQuery(Of Product)("Products", context) For Each result As Product In productQuery1 Console.WriteLine("Product Name: {0}", result.Name) Next Dim queryString As String = "SELECT VALUE product FROM AdventureWorksEntities.Products AS product" ' Call the constructor with the specified query and the ObjectContext. Dim productQuery2 As New ObjectQuery(Of Product)(queryString, context) For Each result As Product In productQuery2 Console.WriteLine("Product Name: {0}", result.Name) Next ' Call the constructor with the specified query, the ObjectContext, ' and the NoTracking merge option. Dim productQuery3 As New ObjectQuery(Of Product)(queryString, context, MergeOption.NoTracking) For Each result As Product In productQuery3 Console.WriteLine("Product Name: {0}", result.Name) Next End UsingBut this did not work I got an error which was the equivalent of
I am therefore confused. Why isn't this working for me?
(p.s. This forum is not emailing the notice of replies to my provided email address. If I don't reply right away, that is why.)
thaicarrot
Contributor
5431 Points
1507 Posts
Re: Attempt to use ObjectQuery does not work
Jan 22, 2013 09:29 PM|LINK
Hi,
Just show you how to convert it back,.
Just convert the Context down 1 level then you can use ObjectQuery. Similiar following.
((IObjectContextAdapter)context).ObjectContext. ObjectStateManager. ChangeRelationshipState(course, instructor, c => c.Instructor, EntityState.Added); Note, that if you are updating (not just adding) a relationship, you must delete the old relationship after adding the new one: ((IObjectContextAdapter)context).ObjectContext. ObjectStateManager. ChangeRelationshipState(course, oldInstructor, c => c.Instructor, EntityState.Deleted);Weera
joeller
Member
171 Points
168 Posts
Re: Attempt to use ObjectQuery does not work
Jan 23, 2013 12:49 PM|LINK
I see your cast but am not exactly sure how it would be used in this case. (E.G. would you case the context upon creation or in the objectquery statement? Particularly as neither of the examples felt the need to cast the context which they actually created as the entities object.)
Can you provide an example that actually uses ObjectQuery and an entities object? (An example in VB would be nice but not absolutely necessary.)
thaicarrot
Contributor
5431 Points
1507 Posts
Re: Attempt to use ObjectQuery does not work
Jan 23, 2013 01:14 PM|LINK
"Value of type 'AdventureWorksEntites' cannot be converted to 'System.Data.Objects.ObjectContext'. "
My understand is that the AdventureWorksEntites derrive from the DbContext.
The ObjectQuery belong to ObjectContext that is why you have to convert the DbContext downward to ObjectContext.
To simple how to convert.
((IObjectContextAdapter)context).ObjectContext
Weera
joeller
Member
171 Points
168 Posts
Re: Attempt to use ObjectQuery does not work
Jan 23, 2013 01:50 PM|LINK
I am not allow to do:
I can only do:
Then that produces "'ToArray()' is not a member of PrjName.recipes", When I try to do:
Array.ForEach(Of Recipe)( recipes.ToArray(), Sub(x) Console.WriteLine( x.RecipeName & " - " & x.RecipeText ) Array.ForEach(Of Bottle)( x.Bottles.ToArray(), Sub(y) Console.WriteLine( y.BoozeFriendlyName ) End Sub) End Sub )thaicarrot
Contributor
5431 Points
1507 Posts
Re: Attempt to use ObjectQuery does not work
Jan 23, 2013 01:57 PM|LINK
I don't know much about VB. You'd better do like this.
1.
private ObjectContext _objectContex;
2.
Then use ObjectQuery with the _objectContext
http://www.developerfusion.com/tools/convert/csharp-to-vb/
Weera
joeller
Member
171 Points
168 Posts
Re: Attempt to use ObjectQuery does not work
Jan 23, 2013 03:12 PM|LINK
Maybe I need someone with some VB skills to answer this.
I see no logical reason why you are creating a separate class member to carry out what is supposed to be a simple LINQ query.
None of the LINQ examples advocate or suggest that.
If LINQ requires all of that then what possible advantage would it serve to use it? I could have finished this twenty times over using straight ADO.net SQLClient objects with standard SQL queries. But I was trying to learn the "new improved" method of data access. If I have to do all of this casting and class member creating just to do a simple Select query, then there is no benefit to the Entity Framework, and Microsoft would be guilty of trying to foist another "Vista" on the development community.
However, I refuse to believe it is all that hard and that all the examples given are wrong. I must be missing something in those examples but I don't know what.
And the first example source provides a further example:
Dim rn As String = "Hot Stuff!!" Dim recipe As Recipe = BoozeContext.Recipes.Where("it.RecipeName = @rn", New ObjectParameter("rn", rn)).Execute(MergeOption.AppendOnly).First() For Each bottle As Bottle In recipe.Bottles Console.WriteLine(bottle.BoozeFriendlyName) NextBut this throws an error reporting:
"Overload resolution failed because no accessible 'Where' accepts this number of arguments."
Has this framework been completely redone from scratch since these examples were written? (But the one from MSDN says it is good for VS 2010).
thaicarrot
Contributor
5431 Points
1507 Posts
Re: Attempt to use ObjectQuery does not work
Jan 23, 2013 06:22 PM|LINK
As I said you're missing my point.
Today, EF has two Context are 1. ObjectContext 2. DBContext.
I am sure you're using DbContext class
_objectContext = ((IObjectContextAdapter)UDbContext).ObjectContext;
You code look like this, don't touch the rest code.
After you cast the DbContext to ObjectContext
Dim recipes as New ObjectQuery( queryString, _objectContext )
Weera
thaicarrot
Contributor
5431 Points
1507 Posts
Re: Attempt to use ObjectQuery does not work
Jan 23, 2013 06:28 PM|LINK
The problem isn't LinQ. The tutorial is ok
The ObjectQuery compatible with ObjectContext not DbContext
1. public partial class DSMSDbContext : DbContext { public DSMSDbContext() : base("name=DSMSDbContext") { this.Configuration.LazyLoadingEnabled = false; }2.
public partial class DSMSDbContext : ObjectContext { public DSMSDbContext() : base("name=DSMSDbContext") { this.Configuration.LazyLoadingEnabled = false; }Weera
joeller
Member
171 Points
168 Posts
Re: Attempt to use ObjectQuery does not work
Jan 24, 2013 05:12 PM|LINK
So you saying the Entity Framework has been changed so that the type of object returned by the call to instantiate a new object of type YadaYadaEntities no longer returns a object compatible with ObjectContext but only with dbContext which then has to be CTyped in order to use it with ObjectQuery.
The change therefore invalidates the example given in MSDN at http://msdn.microsoft.com/en-us/library/bb345303 because it is still shows the variable "context" returned by the instantiation of the entities as being compatibile with an QueryQuery.
And the tutorial given on the other page has also been invalidated because of this change. So i can no longer trust anything written online unless it specifies it is for Entity Framework 5.
So what other breaking changes were made in this upgrade to Entity Framework?
Anyway this issue got to be too much work to be doing merely to dispaly a proof of concept site to the client. I just screw it and resorted to using a SQLDataReader.