Reputation: 316
after carefully researching for a solution to this strange problem that I have and accomplished nothing. I am pretty much desperated.
I am working on a MVC3 project.
I have a DB Context class like this:
//SpaceUpEntities.cs
public class SpaceUpEntities: DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Profile> Profiles { get; set; }
public DbSet<Venue> Venues { get; set; }
public DbSet<Room> Rooms { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Event> Events { get; set; }
public DbSet<Capacity> Capacities { get; set; }
public DbSet<VenueStatus> VenueStatuses { get; set; }
}
and I initialize DBContext when application start
//Global.asax.cs
protected void Application_Start()
{
Database.SetInitializer<SpaceUpEntities>(null);
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
RegisterDependencyInjection();
ModelBinders.Binders.DefaultBinder = new TrimModelBinder();
}
The problem occurs when I want to query/Insert/Update to the DB. For example this piece of code
//SecurityRepository.cs
public RegisterFeedback Register(User user, Profile profile)
{
try
{
using (var entities = new SpaceUpEntities())
{
// Check existing email address
var emailExisted = entities.Users.FirstOrDefault(i => i.Email == user.Email);
//... So on
}
}
catch (Exception e)
{
Logger.Error(e);
return new RegisterFeedback(false, Messages.GeneralError);
}
}
After using (var entities = new SpaceUpEntities())
entities should have been initialized but I ended up with this error
Error Message:
Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.
Initially, I thought it was a connection problem to my server, so I checked:
Although I can hardly see why they are the cause of this problem since I took serveral methods to test my ConnectionString to find it has no issue.
Any help would certainly be appreciated. Thank you in advance.
Thang Do
P/S: this is how I tested my connection string:
This is my connection string
I tested it with this
Upvotes: 1
Views: 296
Reputation: 34347
Make sure you are using the Initialization strategy which matches what you are trying to achieve. Database.SetInitializer<T>(...);
By having the line below in your App_Start
method, I believe you may be bypassing initialization
Database.SetInitializer<SpaceUpEntities>(null);
Here are some options from an article on code first database initialization strategies that I hope will be helpful to you:
There are four different database Initialization strategies:
CreateDatabaseIfNotExists: This is default initializer. As name suggests, it will create the database if none exists as per the configuration. However, if you change the model class and then run the application with this initializer, then it will throw an exception.
DropCreateDatabaseIfModelChanges: This initializer drops an existing database and creates a new database, if your model classes (entity classes) have been changed. So you don’t have to worry about maintaining your database schema, when your model classes change.
DropCreateDatabaseAlways: As the name suggests, this initializer drops an existing database every time you run the application, irrespective of whether your model classes have changed or not. This will be useful, when you want fresh database, every time you run the application, while you are developing the application.
Custom DB Initializer: You can also create your own custom initializer, if any of the above don't satisfy your requirements or you want to do some other process that initializes the database using above initializer
Upvotes: 2