Quelle: dotnetpro
Reverse Engineering mit Entity Framework Core 3.0 17.02.2020, 00:00 Uhr

Sicht zu Source

Die dritte Version des OR-Mappers erzeugt nun auch Programmcode für Views in der Datenbank.
Die Abbildung von Datenbanksichten (Views) hat Entity Framework (EF) Core erst ab Version Core 2.1 partiell unterstützt. Seit Version 3.0 gibt es nun vollständige Unterstützung, auch beim Reverse Engineering. Vor Entity Framework Core 2.1 konnte sich ein Entwickler nur mit einem Work­around behelfen, indem er Sichten im Programmcode wie Tabellen behandelte, siehe Tabelle 1. Beim Forward Engineering müssen Views aber auch weiterhin manuell in der Datenbank mit CREATE VIEW erzeugt werden.
Tabelle 1: Verwendung von Datenbanksichten in Entity Framework Core
ctx.Entry(obj).State System.InvalidOperationException: „Unable to track an instance of type ‚Viewname‘ because it does not have a primary key. Only entity types with primary keys may be tracked.“
ctx.Attach(obj); System.InvalidOperationException: „Unable to track an instance of type ‚Viewname‘ because it does not have a primary key. Only entity types with primary keys may be tracked.“
ctx.Viewname.Local System.InvalidOperationException: „The invoked method is cannot be used for the entity type ‚Viewname‘ because it does not have a primary key.“
p.Property = Wert; ctx.SaveChanges(); Objekt lässt sich im RAM ändern, aber SaveChanges() speichert nichts.
Tabelle 1: Verwendung von Datenbanksichten in Entity Framework Core
ctx.Entry(obj).State System.InvalidOperationException: „Unable to track an instance of type ‚Viewname‘ because it does not have a primary key. Only entity types with primary keys may be tracked.“
ctx.Attach(obj); System.InvalidOperationException: „Unable to track an instance of type ‚Viewname‘ because it does not have a primary key. Only entity types with primary keys may be tracked.“
ctx.Viewname.Local System.InvalidOperationException: „The invoked method is cannot be used for the entity type ‚Viewname‘ because it does not have a primary key.“
p.Property = Wert; ctx.SaveChanges(); Objekt lässt sich im RAM ändern, aber SaveChanges() speichert nichts.

Jetzt 1 Monat kostenlos testen!

Sie wollen zukünftig auch von den Vorteilen eines plus-Abos profitieren? Werden Sie jetzt dotnetpro-plus-Kunde.
  • + Digitales Kundenkonto,
  • + Zugriff auf das digitale Heft,
  • + Zugang zum digitalen Heftarchiv,
  • + Auf Wunsch: Weekly Newsletter,
  • + Sämtliche Codebeispiele im digitalen Heftarchiv verfügbar