我已经在.net框架中为数据访问层创建了mssql db和project(类库)。然后我在asp.net核心创建了项目。
要访问此数据库,我在ConfigureServices
方法中的启动类中使用。我在asp.net核心项目中使用EF核心。
services.AddDbContext<TradeEvoDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("SqlDb")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<TradeEvoDbContext>()
.AddDefaultTokenProviders();
这是我的DbContext类,它引用了我的类库proj。
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TradeEvo.Chat.Identity;
using TradeEvo.DAL.Models;
namespace TradeEvo.Chat.Models
{
public class TradeEvoDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<GlobalSettings> GlobalSettings { get; set; }
public DbSet<UserApiSettings> ApiSettings { get; set; }
public DbSet<UserEBaySettings> EBaySettings { get; set; }
public DbSet<User1CSettings> User1CSettings { get; set; }
public DbSet<UserPayPalSettings> PayPalSettings { get; set; }
public DbSet<CompanyInfo> CompanyInfo { get; set; }
public DbSet<PromInfo> PromInfo { get; set; }
public DbSet<ProductInfoMultiLang> ProductInfoMultiLang { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<PosterImage> PosterImages { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderNote> OrderNotes { get; set; }
public DbSet<OrderItem> OrderItems { get; set; }
public DbSet<OrderItemMont> OrderItemsMont { get; set; }
public DbSet<OrderItemMontKey> OrderItemsMontKey { get; set; }
public DbSet<OrderItemMontLink> OrderItemsMontLink { get; set; }
public DbSet<OrderItemMontFile> OrderItemsMontFile { get; set; }
public DbSet<UserInfo> UserInfo { get; set; }
public DbSet<BlogPost> BlogPosts { get; set; }
public DbSet<BlogComment> BlogComments { get; set; }
public DbSet<TradeEvo.DAL.Models.Chat> Chats { get; set; }
public DbSet<Message> Messages { get; set; }
public DbSet<Status> Status { get; set; }
public DbSet<StatusType> StatusType { get; set; }
public DbSet<StatusTypeMapping> StatusTypeMapping { get; set; }
public DbSet<DistributorProduct> DistributorProducts { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<ProductType> ProductType { get; set; }
public DbSet<ProductContent> ProductContent { get; set; }
public DbSet<ProductInfo> ProductInfo { get; set; }
public DbSet<ProductMontChanges> ProductMontChanges { get; set; }
public DbSet<ProductGenreMapping> ProductGenreMapping { get; set; }
public DbSet<ProductAttribute> ProductAttributes { get; set; }
public DbSet<ProductAttributeValue> ProductAttributeValues { get; set; }
public DbSet<ProductAttributeMapping> ProductAttributeMapping { get; set; }
public DbSet<ProductAttributeCombination> ProductAttributeCombinations { get; set; }
public DbSet<ProductAttributeCombinationMapping> ProductAttributeCombinationMapping { get; set; }
public DbSet<ProductCharacteristic> ProductCharacteristics { get; set; }
public DbSet<ProductCharacteristicValue> ProductCharacteristicValues { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<ProductListAttribute> ProductListAttributes { get; set; }
public DbSet<ProductPromAttribut> ProductPromAttributes { get; set; }
public DbSet<ProductList> ProductLists { get; set; }
public DbSet<ProductListMapping> ProductListMapping { get; set; }
public DbSet<ProductAvailability> ProductAvailability { get; set; }
public DbSet<Currency> Currencies { get; set; }
public DbSet<Language> Languages { get; set; }
public DbSet<UserLanguage> UserLanguages { get; set; }
public DbSet<ResourceMultiLang> ResourceMultiLang { get; set; }
public DbSet<PromUaCategories> PromUaCategories { get; set; }
public DbSet<OrderPromInfo> OrderPromInfo { get; set; }
public DbSet<OrderItemProm> OrderItemProm { get; set; }
public DbSet<Country> Countries { get; set; }
public DbSet<Bank> Bank { get; set; }
public DbSet<RecipientAddress> RecipientAddress { get; set; }
public DbSet<SenderAddress> SenderAddress { get; set; }
public DbSet<OrderInvoice> OrderInvoices { get; set; }
public DbSet<CityNP> CityNP { get; set; }
public DbSet<StreetNP> StreetNP { get; set; }
public DbSet<WarehouseNP> WarehouseNP { get; set; }
public DbSet<Setting> Settings { get; set; }
public DbSet<OrderManufacturer> OrderManufacturs { get; set; }
public DbSet<Purse> Purses { get; set; }
public DbSet<PaymentSystem> PaymentSystem { get; set; }
public DbSet<MoneyMovement> MoneyMovement { get; set; }
public DbSet<MoneyMovementData> MoneyMovementData { get; set; }
public DbSet<MoneyMovementType> MoneyMovementType { get; set; }
public DbSet<ProductMultiDownloadData> ProductMultiDownloadData { get; set; }
public DbSet<Address> Address { get; set; }
public DbSet<Warehouse> Warehouses { get; set; }
public DbSet<ShippingService> ShippingServices { get; set; }
public DbSet<ShippingServiceUserMapping> ShippingServiceUserMapping { get; set; }
public DbSet<ShippingServiceCountryMapping> ShippingServiceCountryMapping { get; set; }
public DbSet<ShippingServiceProductMapping> ShippingServiceProductMapping { get; set; }
public DbSet<UPAddress> UPAddress { get; set; }
public DbSet<UPaddresses> UPaddresses { get; set; }
public DbSet<UPEmails> UPEmails { get; set; }
public DbSet<UPPhones> UPPhones { get; set; }
public DbSet<UPClient> UPClient { get; set; }
public DbSet<NPClient> NPClient { get; set; }
public DbSet<PAdresses> PAdresses { get; set; }
public DbSet<RecipientAddressCustomService> RecipientAddressCustomService { get; set; }
public DbSet<UPShipment> UPShipment { get; set; }
public DbSet<UPParcels> UPParcels { get; set; }
public DbSet<UPParcelItems> UPParcelItems { get; set; }
public DbSet<UPDiscount> UPDiscount { get; set; }
public DbSet<UPDiscountPerClient> UPDiscountPerClient { get; set; }
public DbSet<UPInternationalData> UPInternationalData { get; set; }
public DbSet<UPLifeCycle> UPLifeCycle { get; set; }
public DbSet<ImportExportSettings> ImportExportSettings { get; set; }
public DbSet<ImportExportStructure> ImportExportStructures { get; set; }
public TradeEvoDbContext(DbContextOptions<TradeEvoDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
}
因此,当我尝试访问我的用户表时,例如:
var user = _context.Users.Where(x => x.Id == args[1]).FirstOrDefault();
或者:
var user = await _userManager.Users.FindByIdAsync(args[1]);
我有下一个例外:
{System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNet.Identity.EntityFramework, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. File can not be found.
File name: 'Microsoft.AspNet.Identity.EntityFramework, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
所有堆栈跟踪都是:
{System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNet.Identity.EntityFramework, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Не удается найти указанный файл.
File name: 'Microsoft.AspNet.Identity.EntityFramework, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
at System.Reflection.RuntimeMethodInfo.GetParametersNoCopy()
at System.Reflection.RuntimePropertyInfo.GetIndexParametersNoCopy()
at System.Reflection.RuntimePropertyInfo.GetIndexParameters()
at System.Attribute.GetIndexParameterTypes(PropertyInfo element)
at System.Attribute.InternalGetCustomAttributes(PropertyInfo element, Type type, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](MemberInfo element, Boolean inherit)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.NotMappedMemberAttributeConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnEntityTypeAdded(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnEntityTypeAdded(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.AddEntityType(EntityType entityType)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.AddEntityType(Type type, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(TypeIdentity type, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(Type type, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(Type type)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.FindSets(ModelBuilder modelBuilder, DbContext context)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelCustomizer.FindSets(ModelBuilder modelBuilder, DbContext context)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.<GetModel>b__0(Object k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_1(IServiceProvider p)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_Model()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
at TradeEvo.Chat.Controllers.HomeController.<ChatAsync>d__15.MoveNext() in myproj
我的控制器看起来像:
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger _logger;
private readonly TradeEvoDbContext _context;
public HomeController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
ILogger<HomeController> logger,
TradeEvoDbContext context)
{
_userManager = userManager;
_signInManager = signInManager;
_logger = logger;
_context = context;
}
如果我使用nuget管理器并安装Microsoft.AspNet.Identity.EntityFramework,我将只修复此错误并在此代码行中获得新内容。 System.Web.HttpPostedFile ... File can not be found
我做错了什么?或者如何连接到现有的数据库?我被卡住了。
编辑#1 Asp.net核心项目,csproj文件:
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Owin" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.0-preview2-final" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.1" />
答案 0 :(得分:0)
请检查您的项目参考资料“Microsoft.AspNet.Identity.EntityFramework”库的版本是什么,并将其与web.config文件中的版本进行交叉验证,以获得相同的库。将web.config文件中的版本更改为项目中引用的库的正确版本。这将解决您的问题。
您还可以检查packages.config文件中的版本,
并在web.config中更新版本,如下所示,
答案 1 :(得分:0)
库引用隐式包含该库引用的库。更简单地说,如果您有引用EF 6的库X并且您包含引用EF Core的库Y.然后,库Y将引用两者 EF 6和EF核心。
您可以混合使用EF6 / EF Core,但必须将它们相互隔离。如果您需要引用EF 6的库的某些部分,您必须将其分解到它自己的独立库中,而不引用EF 6。