Reputation: 413
I am building an ASP.NET Core 1.1.0 application that needs to access an existing MySQL database using Entity Framework Core, which I'm building in Visual Studio 2015 with all the latest updates. I started using the "official" MySQL provider (MySql.Data.EntityFrameworkCore), but have the same problem with the Pomelo prvider (Pomelo.EntityFrameworkCore.MySql).
The context object injected into my Home controller has all kinds of NotImplemented exceptions, as you can see in the image. Members is one of my DBSet collections, using the Member POCO. (By the way, I'm not planning on using migrations, so that's not a factor.) This is the state of the context object as soon as it's injected into the controller, before I even try to access data.
The full detail of a typical message is:
((Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure)this.db.Members).Instance' threw an exception of type 'System.NotImplementedException
I don't think there's anything very interesting in my project.json file, but the relevant lines are:
"Pomelo.EntityFrameworkCore.MySql": "1.1.0",
"Pomelo.EntityFrameworkCore.MySql.Design": "1.1.0"
In Startup.cs in the ConfigureServices method, I have these statements:
string connectionString = Configuration.GetConnectionString("ManageRCNConnection");
services.AddDbContext<MembersContext>(options => options.UseMySql(connectionString));
Things are pretty simple at this point.
So far, my Google-foo has let me down trying to figure out what's wrong.
Can anyone give me a clue about what the problem is?
Thanks!
Update: If I ignore the errors in the constructure as @Tseng suggests, I get errors the first time I try to get an entity from the database.
Here is the code for my MembersContext. I've removed a few of the properties, but they are just more DbSet collections.
public class MembersContext : DbContext
{
public MembersContext(DbContextOptions<MembersContext> options) : base(options)
{ }
public DbSet<Member> Members { get; set; }
public DbSet<FamilyMember> FamilyMembers { get; set; }
public DbSet<Membership> Memberships { get; set; }
...
}
I'm not using a context factory, might that be the problem?
Upvotes: 0
Views: 1712
Reputation: 413
Well, this was a frustrating process, but I finally got it all sorted out. Big thanks to @Tseng for the questions and comments, which helped nudge me in the right direction.
First, I switched to using the Pomelo MySQL provider. The official MySQL provider is pre-release, but I thought it was far enough along to at least get started on this new project. It isn't, as of today. (Sapient Guardian also seems to have a solid provider, but I didn't need to try it out.)
Second, I realized that something was just not right about my object context class, and that it was the source of my problems. So I went back to getting scaffolding to work. I tried a number of things, but this is how the relevant parts of the dependencies section of my project.json file ended up:
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
"Microsoft.EntityFrameworkCore.Design": "1.1.0",
"Pomelo.EntityFrameworkCore.MySql": "1.1.1-prerelease-10000",
"Pomelo.Data.MySql": "1.0.0",
"Pomelo.EntityFrameworkCore.MySql.Design": "1.1.1-prerelease-10000" },
That seems to be all the packages required for scaffolding and migrations, although I'm not using migrations. I didn't need to add anything to the tools section of the file.
Then this command worked in the Package Manager Console in Visual Studio 2015:
Scaffold-DbContext "<connection string>" Pomelo.EntityFrameworkCore.MySql -OutputDir Models
Voila! A correct object context as well as entity classes. (I expect the dotnet ef commands would work too, but didn't try them.)
After that, I had a problem with some "Unable to cast object of type 'System.String' to type 'System.Byte'." errors in one entity object. That turned out to be because the provider (or maybe the MySQL connector) has some problems with char fields and strings in .NET Core. I changed those fields to varchar, and all was good.
Upvotes: 1