Exception when trying to use Mysql.Data.Entity

2019-08-23 04:03发布

问题:

I'm trying to get a test project running using MySQL and EF6 using Visual Studio 2019 Community Preview for Mac. I am using the Web Application (Model-View-Controller) / .NET Core -> App template as a starting point as well as the MyWind database. I'm getting the following exception and am not sure how to proceed.

TypeLoadException: Could not load type 'System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider' from assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

    System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetTypeDescriptor(Type type)
    System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(Type type)
    System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
    System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
    System.Data.Entity.Internal.RetryLazy<TInput, TResult>.GetValue(TInput input)
    System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
    System.Data.Entity.Internal.InternalContext.Initialize()
    System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
    System.Data.Entity.Internal.Linq.InternalSet<TEntity>.Initialize()
    System.Data.Entity.Internal.Linq.InternalSet<TEntity>.get_InternalContext()
    System.Data.Entity.Infrastructure.DbQuery<TResult>.System.Linq.IQueryable.get_Provider()
    System.Linq.Queryable.OrderBy<TSource, TKey>(IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
        Ef2MySql.Controllers.HomeController.Contact() in HomeController.cs ViewData["Customers"] = db.Customers.OrderBy(c => c.company).ThenBy(c => c.last_name).ThenBy(c => c.first_name).Take(10);
lambda_method(Closure , object , object[] )
Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, object[] parameters)
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Here's my relevant code.

HomeController.cs

namespace Ef2MySql.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            using (var db = new NorthwindContext())
            {
                ViewData["Customers"] = db.Customers.OrderBy(c => c.company).ThenBy(c => c.last_name).ThenBy(c => c.first_name).Take(10);
            }

            return View();
        }
    }
}

NorthwindContext.cs

namespace Ef2MySql.Database
{
    public partial class NorthwindContext : DbContext
    {
        public NorthwindContext() : base("Server=localhost;Database=northwind;Uid=northwind;Pwd=northwind;")
        {
        }

        public virtual DbSet<Customer> Customers { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
}

NorthwindMysqlConfiguration.cs

namespace Ef2MySql.Database
{
    public class NorthwindMysqlConfiguration : MySqlEFConfiguration
    {
        public NorthwindMysqlConfiguration()
        {
        }
    }
}

Customer.cs

namespace Ef2MySql.DomainObjects
{
    public class Customer
    {
        public Customer()
        {
        }

        public String id { get; set; }
        public String company { get; set; }
        public String last_name { get; set; }
        public String first_name { get; set; }
        public String email_address { get; set; }
        public String job_title { get; set; }
        public String business_phone { get; set; }
        public String home_phone { get; set; }
        public String mobile_phone { get; set; }
        public String fax_number { get; set; }
        public String address { get; set; }
        public String city { get; set; }
        public String state_province { get; set; }
        public String zip_postal_code { get; set; }
        public String country_region { get; set; }
        public String web_page { get; set; }
        public String notes { get; set; } 
    }
}

Here's the version of mono I'm using.

$ mono --version
Mono JIT compiler version 5.18.0.248 (2018-08/a4956c837e1 Fri Jan 25 16:13:12 EST 2019)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
    TLS:           
    SIGSEGV:       altstack
    Notification:  kqueue
    Architecture:  amd64
    Disabled:      none
    Misc:          softdebug 
    Interpreter:   yes
    LLVM:          yes(600)
    Suspend:       preemptive
    GC:            sgen (concurrent by default)

I'm not sure if this is relevant, but for the MySql.Data.Entity and EntityFramework packages, I'm getting the following warning:

Package 'EntityFramework 6.2.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.1'. This package may not be fully compatible with your project.

For what it's worth, I am able to use MySql.Data.MySqlClient.MySqlConnection directly to retrieve data from the database.

回答1:

After doing some research, it seems that this class is simply missing from corefx. It has been added for 3.0.