Showing posts with label EF. Show all posts
Showing posts with label EF. Show all posts

Wednesday, June 29, 2016

Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.

 Problem:

Working a dynamic linq issue.  Using the great helper class, Predicate Builder posted over on the O'Reilly site.  I had built out my query and it worked great against my EF generated class.  Then I started getting this error each time I ran the predicate builder against a data type of nullable Guid.  I searched around on line at some more in depth explanations.  But nothing seemed to really fit my problem.

Solution:

Then I took the simple advice given on stackoverflow.  I changed the following lines
 


var race = Guid.Parse(race_ID);
predicate = predicate.Or(l => l.Race_ID.Equals(race));


to


var race = Guid.Parse(race_ID);
predicate = predicate.Or(l => l.Race_ID==race);

C# interpreter at work here: 

"When == is used on an expression of type object, it'll resolve to System.Object.ReferenceEquals.
Equals is just a virtual method and behaves as such, so the overridden version will be used (which, for string type compares the contents). "


so we could also do




var race = Guid.Parse(race_ID);
predicate = predicate.Or(l => l.Race_ID.Value.Equals(race));

Source:

  1. http://www.albahari.com/nutshell/predicatebuilder.aspx
  2. http://stackoverflow.com/questions/4592432/linq-query-keeps-throwing-unable-to-create-a-constant-value-of-type-system-obje
  3. http://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals

Wednesday, April 27, 2016

System.InvalidOperationException: Migrations is enabled for context but the database does not exist or contains no mapped tables.

Problem:
Entity Framework throws migration exception when trying to move code to Staging Server.


I have been trying out the EF migration and Code First approach on a new project.
I was able to get the migration and update working on my dev box.  I then went to move
the code base to our staging using xcopy.   I then hit the website and got a nasty no mapped
tables error.


Solution:


For me I don't need to move the migrations to the staging or production server.  I just needed to disable the migration.  This can be done using the web.config:




This is the basic syntax for setting an initializer in your config file:
<appSettings>
< add key=”DatabaseInitializerForType MyAssemblyQualifiedContextType”
value=”MyAssemblyQualifedInitializerType” />
< /appSettings>

The key things to notice here are:
  • The entry goes in the appSettings section of your config file
  • The key must always start with the string “DatabaseInitializerForType” followed by whitespace
  • The second part of the key is the assembly-qualified name of the context type for which the initializer should be set
  • The value is the assembly-qualified name of the initializer to set, which must expose a public, parameterless constructor so that EF can create an instance of it
Example:


<add key="DatabaseInitializerForType FCJC.Models.FCJCContext"
value="Disabled"/>


I placed this in my web.staging.config so that each time I build out my solution for deployment to staging, it will insert this flag and no more error!






Source:
https://blog.oneunicorn.com/2011/03/31/configuring-database-initializers-in-a-config-file/