Friday, July 18, 2014

Unable to determine principal end of relationship error in EF

Occasionally, you may get the following error:
Unable to determine the principal end of the 'Model.FK_Parent_Child' relationship. Multiple added entities may have the same primary key.
This is generally caused by attempting to use a primary key property before it has been initialized.  For example.  Let say that you have created the parent and child records, but have not saved either to the database yet.  If the primary key on the parent is an identity field (let's call it 'ParentId', this means that the value has not been initialized.

So if you attempt to link the child to this parent by using this id, you will get the error above:
Child.ParentId = Parent.ParentId
Instead, you want to use the navigation properties to link these records.
Child.Parent = Parent


Friday, June 6, 2014

Determining the amount of space used by all tables in SQL Server

 
SELECT   
   t.NAME AS TableName,  
   s.Name AS SchemaName,  
   p.rows AS RowCounts,  
   SUM(a.total_pages) / 125 AS TotalSpaceMB,   
   SUM(a.used_pages) / 125 AS UsedSpaceMB,   
   (SUM(a.total_pages) - SUM(a.used_pages)) / 125 AS UnusedSpaceMB  
 FROM   
   sys.tables t  
 INNER JOIN     
   sys.indexes i ON t.OBJECT_ID = i.object_id  
 INNER JOIN   
   sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id  
 INNER JOIN   
   sys.allocation_units a ON p.partition_id = a.container_id  
 LEFT OUTER JOIN   
   sys.schemas s ON t.schema_id = s.schema_id  
 WHERE   
   t.NAME NOT LIKE 'dt%'   
   AND t.is_ms_shipped = 0  
   AND i.OBJECT_ID > 255   
 GROUP BY   
   t.Name, s.Name, p.Rows  
 ORDER BY   
   TotalSpaceMB desc 
 

Wednesday, January 29, 2014

Improving bulk insert performance in Entity framework

Sometimes Entity Framework will have poor performance when inserting records. A possible workaround is to set the following flags:
yourContext.Configuration.AutoDetectChangesEnabled = false;
yourContext.Configuration.ValidateOnSaveEnabled = false;
This should be done before the calls to 'AddObject'.   In my case, this reduced the time a piece of code took to process 2,000 records from 8 minutes down to about 10 seconds.

This fix requires more research to determine if there are side-effects to setting these.  If this is a shared context, then the flags should be set back to their original values after 'SaveChanges' has been called.

Note that this only works with DBContext,and not ObjectContext

Monday, January 6, 2014

Moving a Repository from SVN to Git on VisualStudio.com


  1. Create a new folder.
  2. Launch a Git Bash prompt and navigate to this folder.
  3. Use git to clone the SVN repository to this folder. 
    1. git svn clone http://svn/repo/here/trunk   (Taken from http://stackoverflow.com/a/79178/224531)
    2. This may take a while if there is a lot of history.  But the end result is a stand-alone Git repo with all the history in it. 
  4. Go the TFS site (visualstudio.com), and create a new project using Git as the source control. 
  5. After this is created open the project and click on the 'Code' menu.
  6. It should show that the repository is empty.  Follow the steps on this page, using the Bash command prompt, to attach the local repo to this new origin, and push the changes to the TFS site. 

Friday, January 3, 2014

Fixing 500 Errors with IISExpress

When an app throws 500 errors in IIS Express with no additional debugging information, try the following:
  • Close VS Studio - solution set with IISExpress
  • Go to: /Document/IISExpress/config/ in your profile
  • Rename or delete applicationhost.config
  • Open your solution in VS Studio
  • A dialog may fire up from IISExpress - this will set a fresh config.
  • try to run your web app.

Wednesday, November 20, 2013

Fixing Screen Resolution in VirtualBox

Sometimes a virtual pc in VirtualBox will refuse to take up the whole screen.  Even in Full-Screen mode, it will insist on showing things in a 1024x768 resolution.   To fix this, you need to open a command prompt and run one of the following from c:\Program Files\Oracle\VirtualBox .   This was taken from section 9.8.2 of the VirtualBox manual

VBoxManage setextradata global GUI/MaxGuestResolution any
will remove all limits on guest resolutions.
VBoxManage setextradata global GUI/MaxGuestResolution >width,height<
manually specifies a maximum resolution.
VBoxManage setextradata global GUI/MaxGuestResolution auto
restores the default settings. Note that these settings apply globally to all guest systems, not just to a single machine.

Tuesday, September 24, 2013

EF Empty Strings in Non-Nullable Fields

When using Entity Framework, sometimes it is desirable to have non-nullable string fields.  The problem is that if the user leaves the field blank, EF will complain that a value is required in the field.   To fix this, create a metadata class for the entity you are working with.   Then decorate the field with this attribute:
[DisplayFormat(ConvertEmptyStringToNull=false)]
This will allow you to save the entity with an empty string in this field.