1

To do CRUD operation I am adding controller and datacontext in scaffolding Options i have received this error

unable to retrieve metadata unable to cast object of type system.data.entity.core.objects.objectcontext to

This is my Model

namespace MvcMySql.Models
{
public class Code
{
 [Key]
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public string City { get; set; }
}
public class DatabaseContext : DbContext
{
    public DatabaseContext()
        : base("ConnectionString")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Code>().HasKey(p => p.StudentID);
    }
    public DbSet<Code> CodeFirsts { get; set; }
 }
}

My web Config

 <connectionStrings>
<add name="ConnectionString"connectionString="server=10.10.30.164;uid=demouser;password=welcome;database=demo"
providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

 <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=5.0.0.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>
 <providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.SqlClient.SqlProviderServices, EntityFramework.SqlServer" />
  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF5, Version=5.0.0.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>

I cant identify where the error occursthis message i got while doing crud operations

Dhasarathan T
  • 99
  • 1
  • 3
  • 14

1 Answers1

0

I was able to reproduce this issue locally. Then in order to solve it I searched "unrecognized element 'providers' entity framework" on Google as it was the error given on the message box.

The first link suggested me to remove parameter section but it was not present in my web.config file.

I reviewed my web.config files from other projects and found that It does not have a <providers> section in <entityFramework>.

In my current web.config I found it is there (due to downgrade to EF5 from EF6). Providers are introduced from EF6, read here.

Current web.config has:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.SqlClient.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework> 

I removed the provider section and build the solution. Then tried adding the controller using scaffolding and bingo... it worked this time... Now my web.config looks like below:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
Community
  • 1
  • 1
SBirthare
  • 5,117
  • 4
  • 34
  • 59