Showing posts with label EntityFramework. Show all posts
Showing posts with label EntityFramework. Show all posts

Tuesday, July 19, 2016

Viewing the ValidationExceptions Collection

Occassionally, .NET apps that are using EntityFramework will throw the following error

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
The easiest way to see what these exceptions is to add the following to the Watch window:
((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors


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


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

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.

Thursday, May 20, 2010

Converting One Kind of Collection to Another

Getting an ObservableCollection from an IEnumerable (this is the type of loadOperation.Entities

myCollection = new ObservableCollection<myEntityType>(loadOperation.Entities)

// DON’T do this. It returns a null value

//myCollection = (ObservableCollection<myEntityType>)
//loadOperation.Entities;

Converting an iQueryable returned by EntityFramework into a List.  Note that this is an extension method, which means you must be “Using System.Linq” in order have this function available to the iEnumerable.

var myQueryable = (from x in myContext.table select x).ToList()