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

问题描述 投票:0回答:1

我正在尝试使用Visual Studio 2019社区预览版Mac来使用MySQL和EF6运行测试项目。我使用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从数据库中检索数据。

mysql xamarin mono entity-framework-6 connector-net
1个回答
0
投票

经过一些研究,似乎这个类只是missing from corefxIt has been added for 3.0

© www.soinside.com 2019 - 2024. All rights reserved.