MVC3 Database entities don't get initialized as expected

Thang Do

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

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:

  1. ConnectionString
  2. SQL Server setting (SQL Server 2012)

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 ConnectionTest.udl

Chris Ballance

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Adding fields to Laravel 5 authentication don't get inserted into the database

来自分类Dev

How to get perforce to pull only files that don't exist in workspace

来自分类Dev

Best way to get nearby entities

来自分类Dev

How to match catalog.xml entities with database?

来自分类Dev

带有IEnumerable <T>模型的MVC视图来自Linq To Entities-对象处理错误

来自分类Dev

Why don't I get correct result when I use original parseList function in Kotlin?

来自分类Dev

Hibernate create, don't update, don't drop.

来自分类Dev

.net MVC3 BeginTransaction()锁定表

来自分类Dev

什么是mvc3中的ReturnURL

来自分类Dev

在视图上显示MVC3 Unicode

来自分类Dev

Html.BeginForm MVC3

来自分类Dev

在mvc3上保存null DateTime

来自分类Dev

mvc3中的jQuery验证

来自分类Dev

mvc3如何返回存储库

来自分类Dev

MVC3验证摘要控件

来自分类Dev

按钱排序mvc3

来自分类Dev

MVC3和JSON模型绑定

来自分类Dev

mvc3 pagedlist背景选择

来自分类Dev

MVC3和URL篡改

来自分类Dev

How to get the name of class where an object is initialized c#

来自分类Dev

Spring 4 and Hibernate 4/c3p0 with Entity Manager don't start

来自分类Dev

Syntax error, '>' expected MVC razor

来自分类Dev

Replace function of JavaScript don't work

来自分类Dev

Why PhpStorm don't recognizes $.post method?

来自分类Dev

Why don't call method presentationControllerForPresentedViewController of UIViewControllerTransitioningDelegate?

来自分类Dev

Label in UserControl (Winforms) don't show

来自分类Dev

iOS - Background transfer Tasks don't match

来自分类Dev

UNION ALL Query for tables that don't match

来自分类Dev

Don't loop code inside foreach bootstrap