Code First ASP.NET MVC App,Seed方法没有被调用

时间:2016-10-10 21:01:57

标签: c# mysql asp.net asp.net-mvc entity-framework

我跟着this tutorial使用MySQL数据库获得了一个有效的ASP.NET MVC应用程序。我已经完成了它,但发现,在启动我的应用程序时,我覆盖的种子方法永远不会被调用。

/tmp/cx_Oracle-5.2.1$ python setup.py build
running build
running build_ext
building 'cx_Oracle' extension
creating build
creating build/temp.solaris-2.11-sun4v.32bit-2.7-11g
cc -DNDEBUG -KPIC -DPIC -I/u01/app/oracle/product/11.2.0.4/dbhome_1/rdbms/demo -I/u01/app/oracle/product/11.2.0.4/dbhome_1/rdbms/public -I/usr/include/python2.7 -c cx_Oracle.c -o build/temp.solaris-2.11-sun4v.32bit-2.7-11g/cx_Oracle.o -DBUILD_VERSION=5.2.1
unable to execute 'cc': No such file or directory
error: command 'cc' failed with exit status 1

谷歌搜索,我发现我需要在我的应用程序的web.config文件中添加上下文,该文件嵌套在我执行的entityFramework标记中

public class MyDbInitializer : DropCreateDatabaseAlways<MyDbContext>
{
    protected override void Seed(MyDbContext context)
    {
        //Roles
        context.Roles.Add(new Role { role = "admin" });
        context.Roles.Add(new Role { role = "user" });

        //Users
        context.Users.Add(new User { userName = "test", firstName = "Elliot", lastName = "Alderson", email = "ealderson@allsafe.com", phone = "555-555-5555", city = "New York", state = "New York", zipCode = "10001" });

        //User Roles
        context.UserRoles.Add(new UserRole { userName = "test", role = "admin" });

        //Logins
        context.Logins.Add(new Login { userName = "test", password = "test" });

        //Credit Cards
        context.CreditCards.Add(new CreditCard { userName = "test", creditCardNumber = "1234432156788765", expirationDate = new DateTime(2020, 1, 1), svn = "123", brand = "Evil Corp" });

        base.Seed(context);
    }
}

最后,我更改了我的global.asax文件以强制初始化数据库上下文,这也无法正常工作

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="mssqllocaldb" />
    </parameters>
  </defaultConnectionFactory>
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
  <contexts>
    <context type="COS492_SIP.DAL.MyDbContext, COS492_SIP">
      <databaseInitializer type="COS492_SIP.DAL.MyDbInitializer, COS492_SIP" />
    </context>
  </contexts>
</entityFramework>

我认为这与后端数据库是MySQL(我不能切换到另一个)有关,因为entityFramework标签引用了mssql。如果是这样,我该如何解决这个问题,无论哪种方式,我如何调用种子方法?

修改

MyDbContext类

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        System.Data.Entity.Database.SetInitializer<MyDbContext>(new MyDbInitializer());

        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

Web Config连接字符串

public class MyDbContext : DbContext
{

    public MyDbContext() : base("MyDbContextConnectionString")
    {
        Database.SetInitializer<MyDbContext>(new MyDbInitializer());
    }

    public DbSet<CreditCard> CreditCards { get; set; }
    public DbSet<Login> Logins { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<UserRole> UserRoles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

1 个答案:

答案 0 :(得分:1)

Option Explicit Sub main() Dim fso As New FileSystemObject Dim testFolder As Folder Dim f As File Dim i As Long Set testFolder = fso.GetFolder("C:\Users\Ridwan\Desktop\Test_Excel") With Worksheets("Results") For Each f In testFolder.Files If Left(f.Name, 4) = "Test" Then If fso.GetExtensionName(f.Path) = "xlsm" Then With .Cells(.Rows.Count, 1).End(xlUp).Offset(1) .Value = f.Name i = 0 Do i = i + 1 .Offset(, i).Formula = "='" & testFolder.Path & "\[" & f.Name & "]Sheet1'!C" & i + 1 Loop While .Offset(, i) <> 0 .Offset(, i).ClearContents With Range(.Offset(, 1), .Offset(, 1).End(xlToRight)) .Value = .Value End With End With End If End If Next f End With End Sub 中尝试以下内容:

Application_Start