O'Reilly Forums: Problem With Namespaces? - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Problem With Namespaces?

#1 User is offline   chris_H_35495 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 16-September 12

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:

using System;
using System.Data.Objects;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

Have I missed a reference? Or is something else wrong?

Grateful for any assistance.

Chris H
0

#2 User is offline   Dave Navarro 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 20-August 08

Posted 16 September 2012 - 08:59 AM

Hello Chris,

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,

~ Dave
0

#3 User is offline   JulieLerman 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 334
  • Joined: 17-September 08

Posted 16 September 2012 - 03:50 PM

Dave, thanks for helping out! :)

Hi Chris,
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.

Julie
0

#4 User is offline   chris_H_35495 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 16-September 12

Posted 17 September 2012 - 01:16 AM

Hi Julie,

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!

Kind regards,

Chris Hawes

Hi Dave,

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.

Cheers,

Chris
0

#5 User is offline   JulieLerman 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 334
  • Joined: 17-September 08

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:

http://thedatafarm.c...bcontext-usage/
0

#6 User is offline   chris_H_35495 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 16-September 12

Posted 18 September 2012 - 02:05 AM

Hi Julie,

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.

Regards,

Chris Hawes
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users