1

So, I'm fairly new to MVC, and just started following several tutorials on DotNetCurry, more specifically this one but I'm facing this problem that it's already answered

But the answer to it isn't working for me. Here's the context of my test project

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace Sysbat.Models
{
    public class SysbatContext : DbContext
    {
        public DbSet<Objeto> Objetos { get; set; }
        public DbSet<Propiedad> Propeidades { get; set; }
        public DbSet<ObjetoPropiedades> ObjetosPropiedades { get; set; }
        public DbSet<ObjetoValores> ObjetosValores { get; set; }
        public DbSet<PropiedadValores> PorpiedadesValores { get; set; }

        public SysbatContext() : base("DevConn"){}
    }
}

Any idea why I'm also getting the Unable to cast object of type 'System.Data.Entity.Core.Objects.ObjectContext' to 'System.Data.Objects.ObjectContext' when adding a control?

I didn't wanted to raise the dead, as this answer is rom last year, that's why I'm posting a new question.

Update 1:

After further research, I found out this it's not possible using EF 6.1, which I what I was using, and it was either upgrade to mvc 5 or downgrade to EF 5. Given that I'm using VS2010 (my laptop can't handle VS2012 or newer, need to upgrade, XD) MVC 5 its not an option, so I uninstalled EF 6 and installed 5, now, I'm getting another error

Unable to retrieve metadata for 'Sysbat.Models.Objeto'.
Unrecognized element 'providers'.

Here's the class 'Objeto' I'm trying to create the control to

/// <summary>
/// Class that hold the Objetos to be used in the system
/// </summary>
public class Objeto
{
    [Key]
    public int Oid { get; set; }
    public string Nombre { get; set; }
    public DateTime FechaCreacion { get; set; }
}
Community
  • 1
  • 1
CJLopez
  • 5,495
  • 2
  • 18
  • 30
  • Uninformed thought from someone who does entity framework and WPF; entityframework has its concept of a context as being a connection to a DB plus state info. UI databinding has its concept of a context as being the object that controls are databound to. They're usually not the same thing; your model layer has to translate between them. So you can't cast one to the other. – Craig Graham Jul 24 '15 at 14:55

2 Answers2

1

This could be a number of different things. There's really not enough information here to properly diagnose.

I've found that the controller template is extremely finicky in the first place. Sometimes if you haven't rebuilt a certain portion of your solution it will fail. Sometimes it will just fail for no good reason. And, it pretty much only works directly with entity classes and DbContext. If you try to use a view model or you'll be working with something like a repository or service rather than DbContext directly, it will fail.

Long and short, you'll find as you continue to use MVC that it's far more trouble than it's worth. I still use "Add > Controller...", but I simply choose "MVC 5 Controller - Empty" and set up everything myself.

However, if you still want the full controller action and view generation functionality, the best thing I can recommend is to first, clean and rebuild your solution. If it still gives you errors, then close Visual Studio complete and then reopen your solution. Sometimes that's enough to get it to work. Also, make sure that all of your projects in your solution are using the exact same versions of Entity Framework and MVC. You can sometimes get weird behavior if there's version mismatches between projects that reference each other.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
-2

So, I found the answer to this last issue here and now I'm able to add controllers

Edit:

As suggested, I'm adding the solution I found in the link

In the end, the problem was with my connection string. There was a provider specified in my web.config, which I wasn't using, so I only needed to remove the setting and it worked all right!

Before:

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

After

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
Community
  • 1
  • 1
CJLopez
  • 5,495
  • 2
  • 18
  • 30