1

Im using The mysql driver 6.6.4 and EF 4.4.0.0. My context can create the database, It has also created a _MigrationHistory Table, i can insert, List, Delete records.

I can add an action with a create model, and auto create the model, everything is perfect here is my context for claritty

public class MyContext : DbContext
{
    static MyContext()
    {
        Database.SetInitializer<MyContext>(null);
    }

    public MyContext()
        : base("Name=MyContext")
    {
    }

    public DbSet<AdminUser> AdminUsers { get; set; }
    public DbSet<Feedback> Feedbacks { get; set; }
    public DbSet<Navigation> Navigations { get; set; }
    public DbSet<SiteLink> SiteLinks { get; set; }
    public DbSet<SiteNew> SiteNews { get; set; }
    public DbSet<StockList> StockLists { get; set; }
    public DbSet<SubNavigation> SubNavigations { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new AdminUserMap());
        modelBuilder.Configurations.Add(new FeedbackMap());
        modelBuilder.Configurations.Add(new NavigationMap());
        modelBuilder.Configurations.Add(new SiteLinkMap());
        modelBuilder.Configurations.Add(new SiteNewMap());
        modelBuilder.Configurations.Add(new StockListMap());
        modelBuilder.Configurations.Add(new SubNavigationMap());


    }

my constring is the same name as my context, as said this all works. I have also added the Data Provider to my web.config file.

I Have even added some code in the create a stored procedure and this all works fine, bottom line everything looks really good here and as far as im concerned i have done everything right that i could possibly do.

but when I create a controller I get this: [ using the create controller with create, edit, delete options )

enter image description here

Now there is quite a bit of noise about this on here, and on the web but I have actually covered everything. I have even set my dbContext to just be DbSets and nothing else. This has been burning my head for quite some time now and I have done everything and read everything possible before making the move of asking on here.

Now I know this wuld probably be far easier with SQL Server etc etc, however i do not have that option, I have a very good mysql cluster and many sites using EF and code first, problems seem to start when I installed asp.net MVC 4 and upgraded to the latest .net.

Any ideas please, I will try anything

web.config

<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
   <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
 </configSections>
 <connectionStrings>
  <add name="MyContext" connectionString="server=localhost;database=MyDB;user id=myuser; password=mypass; Pooling=false" providerName="MySql.Data.MySqlClient" />
 </connectionStrings>
<appSettings>
  <add key="webpages:Version" value="2.0.0.0" />
  <add key="webpages:Enabled" value="false" />
  <add key="PreserveLoginUrl" value="true" />
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
  <compilation debug="true" targetFramework="4.0" />
  <pages controlRenderingCompatibilityVersion="4.0" clientIDMode="AutoID">
    <namespaces>
      <add namespace="System.Web.Helpers" />
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      <add namespace="System.Web.Mvc.Html" />
      <add namespace="System.Web.Routing" />
      <add namespace="System.Web.WebPages" />
    </namespaces>
  </pages>
</system.web>
<system.data>
  <DbProviderFactories>
       <remove invariant="MySql.Data.MySqlClient" />
     <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.6.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
   </DbProviderFactories>
 </system.data>
   <runtime>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
       <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
       <bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0" />
    </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.6.4.0" newVersion="6.6.4.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
       <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
     </handlers>
       </system.webServer>
      <entityFramework>
      <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
         <parameters>
         <parameter value="v11.0" />
       </parameters>
     </defaultConnectionFactory>
  </entityFramework>
  </configuration>

model:

public class AdminUser
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public short UserLevel { get; set; }
}

mapping

public class AdminUserMap : EntityTypeConfiguration<AdminUser>
{
    public AdminUserMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.UserName)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.Password)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.Email)
            .HasMaxLength(150);

        // Table & Column Mappings
        this.ToTable("AdminUsers", "eyebag");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.UserName).HasColumnName("UserName");
        this.Property(t => t.Password).HasColumnName("Password");
        this.Property(t => t.Email).HasColumnName("Email");
        this.Property(t => t.UserLevel).HasColumnName("UserLevel");
    }
}
davethecoder
  • 3,856
  • 4
  • 35
  • 66
  • 1
    Before creating any controller or whatever, have you tried to compile the solution and run couple of unit tests to see it the code works? After you do that you can add a controller. At least you will know that the problem is not related with any database code. – Kath Nov 27 '12 at 23:21
  • yes i can add records, context creates tables, i can also list added records and delete a record.... everything is perfect bang on, just this little niggle – davethecoder Nov 27 '12 at 23:30
  • Hmm it's definitely because you use non-MSSQL database but probably it will help: http://stackoverflow.com/questions/12165185/mvc4-scaffolding-add-controller-gives-error-unable-to-retrieve-metadata – Kath Nov 27 '12 at 23:32
  • my DB related stuff is all inside a separate class library, i have the source for the mysql connector 6.6.4 this is were most of the code has come from, ( there test project ) mvc was added to the solution after, and this crossed my mind too so i actually created another project and just referenced what i needed ( totally blank project ) and this gave out the same problems :-) – davethecoder Nov 27 '12 at 23:37
  • Ok, what happen if you just go to the Controllers folder and create the empty .cs file, then manually create a new class that inherits from Controller and create some actions and try to build (then you can try to create a View)? – Kath Nov 27 '12 at 23:42
  • Yes, please read initial post, i can create views, and they map fine but i think the way it builds a view from a model, is totally different to how it creates a controller and then the views. as an update i have just ran EF Powertools this again created all the models and the DBContext from my database ( very cool tool ) and all is good, and works but again FLOP on the old generate Controller side of things... nightmare :-/ – davethecoder Nov 27 '12 at 23:49
  • I'm not asking about creating view, I'm asking about if you try to create a Controller manually like a regular CSharp class not using the scaffolding. – Kath Nov 27 '12 at 23:52
  • Yes i create a controller with a create action! right click and created a view, this all works fine :-) – davethecoder Nov 27 '12 at 23:53
  • 1
    Could you post web.config? I had a similar problem once and it was because some configuration sections in web.config. – Ekk Nov 30 '12 at 09:13
  • Can you please post your AdminUser class? I also had a look around the net and found someone else who had an extremely similar problem which they managed to solve by removing the DbContext constructor. Have you tried these? There are also known issues with MySQL and the DatabaseInitializer as well. Can you remove these from your code and try? – Mark Nov 30 '12 at 09:26
  • @ekk post now contains web.config..... mez admin users model is just a normal model, nothing on it, have used entity framework tools now, everything is good just that one thing, have tried everything including what other post etc say. have tried with / without initializer – davethecoder Nov 30 '12 at 09:33
  • can you update your EF to version 5.0 using NuGet? Can you try completely removing EF and install it again it via NuGet? – Mark Nov 30 '12 at 09:36
  • have now added a new context that i made with EF power tools, and new models with mappings, i can create controllers code manually, its just the auto stuff that does not work – davethecoder Nov 30 '12 at 09:37
  • cant upgrade to EF 5, 4.4.0.0 IS EF five but for version 4 of .net, but i have also pulled into vs 2012, and upgraded and used EF 5 and .net 4.5 and still has same problem, – davethecoder Nov 30 '12 at 09:38
  • also did the removing of packages, my own build of mysqldata everything..... this is not a problem with EF, dont forget everything works here, very well with EF 5 code, its creating a controller that does not work, like somewere maybe the TT files are wrong or something – davethecoder Nov 30 '12 at 09:40
  • I highly doubt it has anything to do with the generated TT files. It's more likely that VS does not fully support the MySQL connector when it attempts to generate the views from your database model. I think you can eliminate this as a possible cause by changing your connection string to a SQL Server (or express) instance. – Mark Nov 30 '12 at 09:58
  • ive been doing this with mysql for years though, Visual studio does support it, and im still using visual studio 2010. also there is this https://blogs.oracle.com/MySqlOnWindows/entry/building_an_mvc_3_application however this did not work for me either :-/ – davethecoder Nov 30 '12 at 10:50
  • with the above link, it actually created the databse for me and everything, just the add controller that failed – davethecoder Nov 30 '12 at 10:52
  • You're saying you have been able to insert, select and delete records. How did you test this? – Jeroen Dec 03 '12 at 16:42
  • I guess you already tried to temporarily remove the data connection from the configuration, and then "Add Controller"? – Grimace of Despair Dec 06 '12 at 23:58
  • yes i get an error about missing con string! go figure lol – davethecoder Dec 07 '12 at 16:27

2 Answers2

0

Try these things: Maybe the default port number is somehow wrong so add port number to your connection string to match whatever your server settings are. Reduce your settings to the minumum, removing anything not directly needed for functioning: appSettings, runtime (not sure if this will default correctly), handlers (inside system.webserver).

MrFox
  • 4,852
  • 7
  • 45
  • 81
  • as mentioned, I have re-tried with new totally blank projects, and re-built it line by line, and what you mean by port number wrong? data still goes in everything works, apart from right click --> add controller --> with EF code first create all functions setting – davethecoder Dec 07 '12 at 16:25
  • I think somewere there is a broken link with VS as im not the first person with this error, EF works great, it just wont create anything for you and has a big old flap out – davethecoder Dec 07 '12 at 16:28
0

well it seems that the simplest of solutions to this was to download the nuget package MvcScaffolding, and then use the MVCScaffolding version of add controller with EF create, edit, delete this version works and settles my problems, why the other option does not work will probably be unknown, but thanks for everyones help and sudgestions

davethecoder
  • 3,856
  • 4
  • 35
  • 66