尝试使用 Mysql.Data.Entity 时出现异常

Exception when trying to use Mysql.Data.Entity

我正在尝试使用 MySQL 和 EF6 使用 Visual Studio Mac 的 2019 社区预览来获取测试项目 运行。我使用 Web Application (Model-View-Controller) / .NET Core -> App 模板作为起点以及 MyWind 数据库。我收到以下异常,我不确定如何继续。

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)

这是我的相关代码。

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; } 
    }
}

这是我使用的单声道版本。

$ 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)

我不确定这是否相关,但是对于 MySql.Data.EntityEntityFramework 包,我收到以下警告:

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.

不管怎样,我可以直接使用 MySql.Data.MySqlClient.MySqlConnection 从数据库中检索数据。

经过一番研究,这个class似乎就是missing from corefx. It has been added for 3.0