Ich habe das gleiche Problem. Ich habe die folgenden Abhängigkeiten in meine project.json
Datei hinzugefügt:
"dependencies": {
"EntityFramework": "7.0.0-beta4",
"EntityFramework.Core": "7.0.0-rc1-final",
"EntityFramework.SqlServer": "7.0.0-beta8",
"EntityFramework.Commands": "7.0.0-rc1-final"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel",
"ef": "EntityFramework.Commands"
},
...
Ich habe dnu install EntityFramework
und dnu install EntityFramework.SqlServer
installiert, um die Pakete zu installieren. "EntityFramework": "7.0.0-beta4",
wurde automatisch vom Installer selbst unter dependencies
geschrieben.
Problem / Frage 1: Zu meiner Überraschung, als ich die Intellisence für EntityFramework
die verfügbare Version, die ich vorstelle, nur 6.1.3
!
dnu build
2: Wenn ich meine Anwendung mit Hilfe von dnu build
kompiliere (Ich dnu restore
Befehl dnu restore
ausgeführt, nachdem ich EntityFramework
Assemblies ( .Core
und .Commands
) manuell unter dependencies
Knoten hinzugefügt habe, EntityFramework
ich eine Menge Kompilierungsfehler:
The type 'DbContextOptionsBuilder' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=null'.
D:\Projects\aspnet\apiservice\Models\SampleData.cs(12,41): DNXCore,Version=v5.0 error CS0308: The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments
D:\Projects\aspnet\apiservice\Models\SampleData.cs(13,32): DNXCore,Version=v5.0 error CS0308: The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments
D:\Projects\aspnet\apiservice\Models\SampleData.cs(14,29): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'Books' and no extension method 'Books' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
D:\Projects\aspnet\apiservice\Models\SampleData.cs(15,42): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'Authors' and no extension method 'Authors' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
D:\Projects\aspnet\apiservice\Models\SampleData.cs(17,43): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'Authors' and no extension method 'Authors' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
D:\Projects\aspnet\apiservice\Models\SampleData.cs(19,45): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'Authors' and no extension method 'Authors' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
D:\Projects\aspnet\apiservice\Models\SampleData.cs(22,29): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'Books' and no extension method 'Books' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
D:\Projects\aspnet\apiservice\Models\SampleData.cs(61,29): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'SaveChanges' and no extension method 'SaveChanges' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Wenn ich die .Core
und .Commands
Assembly-Referenzen .Commands
das Projekt .Core
.Commands
.
Dann habe ich die Version für EntityFramework.Core
auf 7.0.0.0
wie im Fehler erwähnt geändert:
The type 'DbContextOptionsBuilder' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=null'.
Aber dnu restore
gab mir jetzt folgendes:
Unable to locate Dependency EntityFramework.Core >= 7.0.0
Writing lock file D:\Projects\aspnet\apiservice\project.lock.json
Restore complete, 6675ms elapsed
Errors in D:\Projects\aspnet\apiservice\project.json
Unable to locate Dependency EntityFramework.Core >= 7.0.0
Ich bin ein Neuling in asp.net 5
Visual Studio code
Plattform. Und ich kann Visual Studio 2015
nicht verwenden, da andere Mitglieder des Entwicklerteams OSX verwenden.
Dies ist ein Lernprojekt für mich. Anforderung ist ziemlich geradlinig. Verbinden Sie eine SQL Server-Datenbank, ziehen Sie relationale master-child
Daten mit Entity Framework.
Verschraubt! Hilfe!!
Danke im Voraus
AKTUALISIEREN
Ich kann die großen Änderungen in project.json
für alle Pakete mit Version 7.0.0-rc1-final
. Ich habe Yeoman-Generator-Tool verwendet, um das Projekt zu erstellen, aber es sieht so aus, als ob Sie das Paket über Visual Studio
korrigiert haben. Muss ich die Paketversion manuell aktualisieren und restore
?
Wenn das Projekt von Yeoman erstellt / generiert wurde, wurde standardmäßig keine global.json
Datei hinzugefügt. Kann ich das manuell hinzufügen und den Code manuell eingeben? Ob das funktioniert?
Ich kann auch sehen, dass einige zusätzliche Paketverweise zur project.json
Datei hinzugefügt werden, die nicht standardmäßig von Yeoman Generator hinzugefügt wurden - wie Microsoft.AspNet.Tooling.Razor
, Microsoft.Extensions.Logging.Console
, Microsoft.Extensions.Logging.Debug
, Microsoft.Extensions.Logging
usw. Werden alle im Projekt effektiv genutzt? Da der Generator sie nicht automatisch hinzugefügt hat, kann ich sie manuell hinzufügen und mithilfe von dnu restore
. Wird dies Auswirkungen auf das Projekt haben, wenn ich sie manuell hinzufüge?
Sie sollten entfernen "EntityFramework"
von Abhängigkeiten (entfernen Sie die Zeile "EntityFramework": "7.0.0-beta4"
und Nutzung)
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final"
nur. Sie müssen "EntityFramework.Core": "7.0.0-rc1-final"
explizit hinzufügen, wenn Sie "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final"
aufgrund der folgenden Abhängigkeiten hinzugefügt haben:
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final":
"EntityFramework.Relational": "7.0.0-rc1-final":
"EntityFramework.Core": "7.0.0-rc1-final"
Ich meine, dass EntityFramework.MicrosoftSqlServer
erfordert EntityFramework.Relational
, die EntityFramework.Core
erfordert.
Sie sollten zusätzlich überprüfen, dass global.json
"sdk": { "version": "1.0.0-rc1-update1" }
enthält "sdk": { "version": "1.0.0-rc1-update1" }
.
AKTUALISIERT : Ich habe das von Ihnen hochgeladene Testprojekt untersucht. Es hatte viele kleine Probleme. Ich habe es modifiziert. Sie können das geänderte Projekt hier herunterladen. Vergessen Sie nicht, den in appsettings.json
verwendeten ConnectionString
zu appsettings.json
, er enthält den Server
den ich bei mir benutzt habe.
Die wichtigsten Änderungen sind die Verwendung von einfacheren BookContext.cs
:
using Microsoft.Data.Entity;
namespace apiservice.Models {
public class BookContext : DbContext {
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
}
}
die Verwendung der folgenden SampleData.cs
using System;
using System.Linq;
using Microsoft.Data.Entity;
using Microsoft.Extensions.DependencyInjection;
namespace apiservice.Models
{
public static class SampleData
{
public static void Initialize(IServiceProvider serviceProvider) {
var context=serviceProvider.GetService<BookContext>();
context.Database.Migrate();
if (!context.Books.Any()) {
var austen = context.Authors.Add(
new Author { LastName = "Austen", FirstName = "Jane" }).Entity;
var dickens = context.Authors.Add(
new Author { LastName = "Dickens", FirstName = "Charles" }).Entity;
var cervantes = context.Authors.Add(
new Author { LastName = "Cervantes", FirstName = "Miguel" }).Entity;
context.Books.AddRange(
new Book {
Title = "Pride and Prejudice",
Year = 1813,
Author = austen,
Price = 9.99M,
Genre = "Comedy of manners"
},
new Book {
Title = "Northanger Abbey",
Year = 1817,
Author = austen,
Price = 12.95M,
Genre = "Gothic parody"
},
new Book {
Title = "David Copperfield",
Year = 1850,
Author = dickens,
Price = 15,
Genre = "Bildungsroman"
},
new Book {
Title = "Don Quixote",
Year = 1617,
Author = cervantes,
Price = 8.95M,
Genre = "Picaresque"
}
);
context.SaveChanges();
}
}
}
}
die Verwendung von project.json
mit folgenden dependencies
:
{
"dependencies": {
"Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc1-final",
"Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final",
"System.Net.Http": "4.0.1-beta-23516",
"Microsoft.Net.Http": "2.2.29",
"Newtonsoft.Json": "8.0.1"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel",
"ef": "EntityFramework.Commands"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}
Fixieren von appsettings.json
aus
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
},
"Data": {
"DefaultConnection":{
"ConnectionString": "Server=localhost;Database=BookStore;User Id=sa; Password=******;Trusted_Connection=true;"
}
}
}
}
zu
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
}
},
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=localhost;Database=BookStore;User Id=sa; Password=******;Trusted_Connection=true;"
}
}
}
( Data
sollten nicht unter Logging
)
und die Verwendung der folgenden Startup.cs
using apiservice.Models;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Data.Entity;
namespace apiservice
{
public class Startup
{
public static IConfigurationRoot Configuration {get; set;}
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<BookContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
);
services.AddMvc();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
// we need to execute the following two commands before
// dnu restore
// dnx ef migrations add Initial
// dnx ef database update
// For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
try
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
serviceScope.ServiceProvider.GetService<BookContext>()
.Database.Migrate();
}
}
catch { }
app.UseIISPlatformHandler();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
SampleData.Initialize(app.ApplicationServices);
}
// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
}
Ich habe im Kommentar innerhalb der Configure
Methode geschrieben, dass man die Befehle ausführen soll
dnx ef migrations add Initial
dnx ef database update
nachdem alle Pakete wiederhergestellt sind. dnx ef migrations add Initial
erstellt zusätzliche Migrations
Ordner im Projekt mit den Dateien wie 20160101205609_Initial.cs
und BookContextModelSnapshot.cs
. Die in appsettings.json
definierte appsettings.json
wird von dnx ef database update
und während des SampleData.cs
mit dem Programm mit Testdaten von SampleData.cs
gefüllt.
Übrigens habe ich package.json
in den package.json
verschoben (auf derselben Ebene wie project.json
). Sie verwenden die Pakete nicht, aber die Bewegung macht die npm-Pakete sichtbar und verwaltet sie in Visual Studio.