Entity Framework - Methods missing from Entities Object

by Johnny Nouel 22. marzo 2011 20:02

I had a difficult time trying to decide the title of this post.  I couldn't decide between the current title and "Entities from outer space invade .NET and steal its methods".  Of course I know you agree with my current selection.

The thing is that I'm actually kind of new to the EF ORM.  And as I go along using it in my new project issues and discoveries come to my attention.  Some of them are plain silly and others hidden even from Google and Bing (or so I thought).

I developed a Class Library which contains the EF Model for one of the databases I'm working on.  I then add this library as a reference in another project in order to consume it.  The thing is that the Entities Object was not exposing some of the properties or methods that were available in the Class Library.

Let's suppose I just built a Library that works against the Pubs Database.  I added a reference to this project in a Windows Forms project.

Add Reference Dialog

I have a method called GetData that instantiates the pubsEntities object and when Intellisense does its magic some methods like SaveChanges are missing.  Also, the properties and methods of tables are gone as well.  Take a look:

db Object Intellisense

The SaveChanges method is nowhere to be seen.  At first I tried to figure it out without trying to compile the application but the problem was staring at my face and did not want to see it.  After compiling and hovering the mouse over the underlined issues in the code you could easily read that an assembly was missing.

Error in Visual Studio

Yes.  That's right! All you have to do is add the System.Data.Entity assembly to the project consuming the EF Class Library and voila!  All methods are back!

The code now looks ok (it does now that I changed the method name from GetData to AddSomeAuthor):

private void AddSomeAuthor()
{
    Pubs.pubsEntities db = new Pubs.pubsEntities();
    Pubs.author author= new Pubs.author();
    author.au_id="JN";
    db.authors.AddObject(author);
    db.SaveChanges();
}

Cheers! Winking smile

Tags: , , ,

ASP.NET | EntityFramework

Agregar Comentario




  Country flag
biuquote
  • Comentario
  • Vista Previa
Loading


Recent Posts

Sobre mi

Johnny Nouel Mi nombre es Johnny Nouel. Profesional de Tecnología.  Experto en SAP FI CO.  Especialista en SAP BW, BI y BO.  Desarrollador ASP.NET.  Amante de los Videojuegos y la Tecnología.  Aguilucho!

Contactame?