Saturday, September 6, 2014

Some overview of Entity Framework implementation

  1.         private agsEntities agsEntities = new agsEntities(connectionstring);
  2.  
  3.         public ObservableCollection<FAMILY> FamilyCollection.....
  4.         ..........INotify
  5.  
  6.  
  7.         public ObjectQuery<FAMILY> FamiliesQuery;
  8. // FAMILY class is defined as:      public partial class FAMILY : EntityObject
  9. // mostly this was autocreated by EF
  10.         public ObjectQuery<FAMILY> GetFAMILIESQuery(agsEntities agsEntities)
  11.         {
  12.             ObjectQuery<FAMILY> fAMILIESQuery = agsEntities.FAMILIES;
  13.             return fAMILIESQuery;
  14.         }
  15.  
  16.         private CollectionViewSource _familiesViewSource;
  17.         public CollectionViewSource FamiliesViewSource
  18.         {
  19.             get
  20.             {
  21.                 if (_familiesViewSource == null)
  22.                     GetFamilyViewSource();
  23.                 return _familiesViewSource;
  24.             }
  25.         }
  26.  
  27.         private void GetFamilyViewSource()
  28.         {
  29.             // Load data into FAMILIES
  30. //this is ObjectQuery<T>
  31.             this.FamiliesQuery = this.GetFAMILIESQuery(this.agsEntities);
  32. //this is a CollectionViewSource......
  33.             this._familiesViewSource = new CollectionViewSource();
  34. //this is an ObjectResult<T>
  35.             this._familiesViewSource.Source = this.FamiliesQuery.Execute(MergeOption.AppendOnly);
  36. //refresh the view (in this case a DataGrid is bound to FamiliesViewSource
  37.             this._familiesViewSource.View.Refresh();
  38. //and into an observable collection just in case
  39. //agsEntities.FAMILIES is defined by EF as:       public ObjectSet<FAMILY> FAMILIES
  40.             foreach (FAMILY thing in agsEntities.FAMILIES)
  41.                 FamilyCollection.Add(thing);
  42.  
  43.         }

Friday, September 5, 2014

Installing/Using Entity Framework on Visual Studio 2010 with NPGSQL DDEXprovider extension

Win 7 - 64 bit
Visual Studio 2010 Ultimate SP1
.NET 4.0 only.

Entity Framework 6.1.30610.0, NpgSQL version 2.2.0.0 , also includes Mono.Security.
http://www.mediafire.com/download/b6nti9axr0302i6/NpgSQL-EF.rar

DDEX Provider Extension ( plugin / add-in ) for VS 2010 only (none other)
- adds PostgreSQL to the Add Item | Create ADO.NET Entity Data Model menu.
http://www.mediafire.com/download/c55qcsocoqktq5t/NpgsqlDdexProvider.rar

GACUtil
http://www.mediafire.com/download/33mfozceo9gh6jl/gacutil.rar

Step 1: Extract the files from the DLL. Copy Npgsql.dll and Mono.Security.dll to an important system folder, like program files or windows, because they will be going in the system-wide GAC (Global Assembly Cache) and you dont want to delete them by accident if you had them in Downloads\stupidname.

Step 2:
RunVisual Studio 2010 as Administrator, click "Tools | Visual Studio Command Prompt" (this launches an elevated / admin command prompt, WITH the proper PATH Variables to find gacutil. If you cannot find gacutil, I have provided it, it is also located in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\
Make sure you point it to the right path- take note that program files is administrator only. Run the following command to Install to the GAC.

gacutil -i "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Npgsql40\Npgsql.dll"
gacutil -i "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Npgsql40\Mono.Security.dll"

Step 3:
Extract NpgsqlDdexProvider.rar anywhere and Double Click the NpgsqlDdexProvider.vsix file (in  to load the provider into the menu)

Step 4:
In the same Admin Command Prompt, run notepad.exe and edit the machine.config (needs admin)

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config


Search for <DbProviderFactories>  inside <system.data> and add the following line. Take note that the existing entry has a closing </DbProviderFactories> tag on the end of the line. You must add it before this. Formatting it with Enter, Tabs, and spaces are ok, like XML code.

    <add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=2.2.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" support="FF" />
For example, This is the way it is written on my own computer:

<system.data>
<DbProviderFactories>
<add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
<add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=2.2.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" support="FF" />
</DbProviderFactories>
</system.data>

Step 5:
Open your visual studio solution / project that you want to use. Right click "Add references" and locate and add 4 of the 6 DLL files you extracted from NpgSQL-EF.rar (Mono.Security is not needed, Legacy is not needed) - so you need these 4: EntityFramework.dll , EntityFramework.SqlServer.dll , Npgsql.dll , Npgsql.EntityFramework.dll

Step 6:
A new file named "App.config" was created at the bottom of your project. Locate the <providers> section and add this provider:
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
If you need to use legacy, for some reason, add:
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFrameworkLegacy" />

Step 7:
Re-Build your project. "Build menu | Rebuild"


Step 8:
Right Click your PROJECT itself in solution explorer.
Add -> New Item...
ADO.NET Entity Data Model
Now, Follow these nice Instructions made for 2013, the first picture has 4 icons instead of 2, so you want to:
"Generate From Database"
https://github.com/npgsql/Npgsql/wiki/Visual-Studio-Design-Time-Support---DDEX-Provider#new-adonet-entity-data-model


(Where I got the procedure from - written for 2013: https://github.com/npgsql/Npgsql/wiki/Visual-Studio-Design-Time-Support---DDEX-Provider )

It took me a very long time to figure this out, alot of errors, I couldnt find the extension, I needed to compile it myself, and I needed Visual Studio 2010 SP1 SDK to do it.