Portieren von EF 6 nach EF CorePorting from EF6 to EF Core
Aufgrund der wesentlichen Änderungen in EF Core wird nicht empfohlen, eine EF 6-Anwendung auf EF Core umzustellen – es sei denn, es gibt einen zwingenden Grund für eine solche Änderung.Because of the fundamental changes in EF Core we do not recommend attempting to move an EF6 application to EF Core unless you have a compelling reason to make the change. Sie sollten den Wechsel von EF 6 nach EF Core weniger als Upgrade, sondern als eine Portierung betrachten.You should view the move from EF6 to EF Core as a port rather than an upgrade.
Wichtig
Bevor Sie die Portierung starten, müssen Sie überprüfen, ob EF Core den Datenzugriffsanforderungen für Ihre Anwendung entspricht.Before you start the porting process it is important to validate that EF Core meets the data access requirements for your application.
Fehlende FeaturesMissing features
Stellen Sie sicher, dass EF Core über alle Funktionen verfügt, die Sie in Ihrer Anwendung benötigen.Make sure that EF Core has all the features you need to use in your application. Im Funktionsvergleich finden Sie einen ausführlichen Vergleich der Funktionen in EF Core mit EF6.See Feature Comparison for a detailed comparison of how the feature set in EF Core compares to EF6. Wenn erforderliche Features fehlen, stellen Sie vor dem Portieren zu EF Core sicher, dass dies kompensiert werden kann.If any required features are missing, ensure that you can compensate for the lack of these features before porting to EF Core.
VerhaltensänderungenBehavior changes
Dies ist eine nicht vollständige Liste einiger Verhaltensänderungen zwischen EF6 und EF Core.This is a non-exhaustive list of some changes in behavior between EF6 and EF Core. Berücksichtigen Sie diese beim Portieren Ihrer Anwendung, da hierdurch das Verhalten der Anwendung geändert wird, dies aber nach der Umstellung auf EF Core nicht als Kompilierungsfehler angezeigt wird.It is important to keep these in mind as your port your application as they may change the way your application behaves, but will not show up as compilation errors after swapping to EF Core.
Dbset.Add/Attach und Graph-VerhaltenDbSet.Add/Attach and graph behavior
In EF6 führt das Aufrufen von DbSet.Add()
für eine Entität zu einer rekursiven Suche nach allen Entitäten, auf die in den Navigationseigenschaften verwiesen wird.In EF6, calling DbSet.Add()
on an entity results in a recursive search for all entities referenced in its navigation properties. Alle Entitäten, die gefunden werden und nicht bereits vom Kontext nachverfolgt werden, werden ebenfalls als hinzugefügt gekennzeichnet.Any entities that are found, and are not already tracked by the context, are also marked as added. DbSet.Attach()
verhält sich identisch, mit dem Unterschied, dass alle Entitäten als unverändert markiert werden.DbSet.Attach()
behaves the same, except all entities are marked as unchanged.
EF Core führt eine ähnliche rekursive Suche aus, allerdings mit etwas anderen Regeln.EF Core performs a similar recursive search, but with some slightly different rules.
- Die Stammentität befindet sich immer im angeforderten Zustand (hinzugefügt für
DbSet.Add
und unverändert fürDbSet.Attach
).The root entity is always in the requested state (added forDbSet.Add
and unchanged forDbSet.Attach
). - Für Entitäten, die während der rekursiven Suche von Navigationseigenschaften gefunden werden:For entities that are found during the recursive search of navigation properties:
- Wenn der Primärschlüssel der Entität im Speicher generiert wirdIf the primary key of the entity is store generated
- Wenn der Primärschlüssel nicht auf einen Wert festgelegt ist, wird der Zustand auf hinzugefügt festgelegt.If the primary key is not set to a value, the state is set to added. Der Primärschlüsselwert wird als nicht festgelegt betrachtet, wenn ihm der CLR-Standardwert für den Eigenschaftentyp zugewiesen ist (z. B.
0
fürint
,null
fürstring
usw.).The primary key value is considered "not set" if it is assigned the CLR default value for the property type (for example,0
forint
,null
forstring
, etc.). - Wenn der Primärschlüssel auf einen Wert festgelegt ist, wird der Zustand auf unverändert festgelegt.If the primary key is set to a value, the state is set to unchanged.
- Wenn der Primärschlüssel nicht auf einen Wert festgelegt ist, wird der Zustand auf hinzugefügt festgelegt.If the primary key is not set to a value, the state is set to added. Der Primärschlüsselwert wird als nicht festgelegt betrachtet, wenn ihm der CLR-Standardwert für den Eigenschaftentyp zugewiesen ist (z. B.
- Wenn der Primärschlüssel nicht von der Datenbank generiert wird, wird die Entität in denselben Zustand wie der Stamm versetzt.If the primary key is not database generated, the entity is put in the same state as the root.
- Wenn der Primärschlüssel der Entität im Speicher generiert wirdIf the primary key of the entity is store generated
Code First-DatenbankinitialisierungCode First database initialization
EF6 verfügt über eine Vielzahl von Kriterien zum Auswählen der Datenbankverbindung und Initialisieren der Datenbank. Zu diesen Regeln gehören:EF6 has a significant amount of magic it performs around selecting the database connection and initializing the database. Some of these rules include:
Wenn keine Konfiguration durchgeführt wird, wählt EF6 eine Datenbank in SQL Express oder LocalDb aus.If no configuration is performed, EF6 will select a database on SQL Express or LocalDb.
Wenn eine Verbindungszeichenfolge mit dem gleichen Namen wie der Kontext in der
App/Web.config
-Datei der Anwendung vorhanden ist, wird diese Verbindung verwendet.If a connection string with the same name as the context is in the applicationsApp/Web.config
file, this connection will be used.Wenn die Datenbank noch nicht vorhanden ist, wird sie erstellt.If the database does not exist, it is created.
Wenn keine der Tabellen aus dem Modell in der Datenbank vorhanden ist, wird das Schema für das aktuelle Modell der Datenbank hinzugefügt.If none of the tables from the model exist in the database, the schema for the current model is added to the database. Wenn Migrationen aktiviert sind, werden sie verwendet, um die Datenbank zu erstellen.If migrations are enabled, then they are used to create the database.
Wenn die Datenbank vorhanden ist und das Schema bereits von EF6 erstellt wurde, wird das Schema auf Kompatibilität mit dem aktuellen Modell geprüft.If the database exists and EF6 had previously created the schema, then the schema is checked for compatibility with the current model. Wenn sich das Modell seit der Erstellung des Schemas geändert hat, wird eine Ausnahme ausgelöst.An exception is thrown if the model has changed since the schema was created.
EF Core führt diese Aktionen nicht aus.EF Core does not perform any of this magic.
Die Datenbankverbindung muss explizit im Code konfiguriert werden.The database connection must be explicitly configured in code.
Es wird keine Initialisierung ausgeführt.No initialization is performed. Sie müssen
DbContext.Database.Migrate()
verwenden, um Migrationen anzuwenden (oderDbContext.Database.EnsureCreated()
undEnsureDeleted()
, um die Datenbank ohne Migrationen zu erstellen bzw. zu löschen).You must useDbContext.Database.Migrate()
to apply migrations (orDbContext.Database.EnsureCreated()
andEnsureDeleted()
to create/delete the database without using migrations).
Benennungskonvention für Code First-TabelleCode First table naming convention
EF6 führt den Entitätsklassennamen über einen Pluralisierungsdienst aus, um den Standardtabellennamen zu berechnen, dem die Entität zugeordnet wird.EF6 runs the entity class name through a pluralization service to calculate the default table name that the entity is mapped to.
EF Core verwendet den Namen der DbSet
-Eigenschaft, in der die Entität für den abgeleiteten Kontext verfügbar gemacht wird.EF Core uses the name of the DbSet
property that the entity is exposed in on the derived context. Wenn die Entität nicht über eine DbSet
-Eigenschaft verfügt, wird der Klassenname verwendet.If the entity does not have a DbSet
property, then the class name is used.