Jump to content
Problem With Namespaces?
5 replies to this topic
Posted 16 September 2012 - 07:43 AM
I have looked through the posts but can't see a reference to my problem.
I have just started going through the book, 2nd Edition and am having trouble with the first query example, 3.1. It runs fine as written in 3.1, but when I try to change "var contacts = context.Contacts;" to "ObjectSet<Contact> contacts = context.Contacts;" I get the following error:
Error 2 Cannot implicitly convert type 'System.Data.Entity.DbSet<Chapter2ConsoleApp.Contact>' to 'System.Data.Objects.ObjectSet<Chapter2ConsoleApp.Contact>' c:\users\chris\documents\visual studio 2012\Projects\Chapter2ConsoleApp\Chapter2ConsoleApp\Program.cs 25 47 Chapter2ConsoleApp
I have included:
Have I missed a reference? Or is something else wrong?
Grateful for any assistance.
Posted 16 September 2012 - 08:59 AM
Whenever I create an Objectset I write my statement like this;
ObjectSet<Contacts> contacts= this.ObjectContext.CreateObjectSet<Contacts>();
This is written in my data service.cs class.
If I was going to write a statement like you showed in your example - "contacts = context.Contacts;" I'd write it directly in my code behind or ViewModel. I'd simply add the 'var' to the beginning and be set.
The reason I'd do it like this is because I'd expect my context to already have entities... or data, so I don't have to go back to the server / database again unless I needed to for some specific reason.
I hope this helps,
Posted 16 September 2012 - 03:50 PM
Dave, thanks for helping out!
You must be using VS2012. By default VS2012 generates DbContext & DbSet, not ObjectContext and ObjectSet.
DbSet is esssentially a wrapper around ObjectSet that makes things a lot easier to use. Unfortunately, since my 2nd edition is 2 years old that will create some confusion. And as you go thorugh the book where things are focused on the more detailed ObjectSet, there will be differences.
You might want to read the much newer Programming Entity Framework DbContext before going through 2nd edition so that you will know when to shift some things in the existing code. Then you can use the 2nd edition to dig into the informaiton on querying with linq to entities, dealing with performance, application samples, etc with the knowledge about DbContext & DbSet in your head.
I'm sorry about this. I've thought a lot about updating the book, but right now things are shifting so quickly with EF now being open source, that it's a difficult problem.
Posted 17 September 2012 - 01:16 AM
Thank you very much for your prompt and helpful reply. Yes, I am using VS2012. I have your DbContext book so I'll have a look at that before deciding what to do - I might just go back to VS2010 for a while. Ain't progress wonderful!
Thanks for your suggestions. I need to get my head round all of this since although I have been using VS and C# for years, I am new to EF.
Posted 17 September 2012 - 05:22 AM
Personally I think that if you can get your head around dbcontext, you can go back to the 2nd edition to the relevant chapters that go indepth into things that the dbcontext book doesn't (i.e. querying) without having to use VS2010.
p.s. here's a relevant blog post I wrote about the new default code gen:
Posted 18 September 2012 - 02:05 AM
I'm going to take your advice and look at DbContext with VS2012. I'll let you know how I get on when I go back to the 2nd Edition.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users