Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Unix & Linux ServerPeppermintOS Is Moving From Xorg to XLibre to Avoid Wayland(22.09.2026 um 19:58 Uhr)
Sicherheitslücken (CVE)USN-8803-1: Sudo vulnerability(22.09.2026 um 16:15 Uhr)
Sichere ProgrammierungClaude Opus 5.5 is now available in GitHub Copilot(22.09.2026 um 19:10 Uhr)
Sichere ProgrammierungColab is now part of your Google AI plan(22.09.2026 um 20:51 Uhr)
Sichere ProgrammierungThe Hidden Production Risks of Third-Party SDKs(22.09.2026 um 20:00 Uhr)
Unix & Linux ServerPeppermintOS Is Moving From Xorg to XLibre to Avoid Wayland(22.09.2026 um 19:58 Uhr)
Sicherheitslücken (CVE)USN-8803-1: Sudo vulnerability(22.09.2026 um 16:15 Uhr)
Sichere ProgrammierungClaude Opus 5.5 is now available in GitHub Copilot(22.09.2026 um 19:10 Uhr)
Sichere ProgrammierungColab is now part of your Google AI plan(22.09.2026 um 20:51 Uhr)
Sichere ProgrammierungThe Hidden Production Risks of Third-Party SDKs(22.09.2026 um 20:00 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

onModelCreating

As vezes é necessário configurações e comportamentos adicionais em seus modelos e banco de dados. Caso seja necessário, podemos utilizar o onModelCreating. É um método especial do DbContext que existe pra você configurar qualquer coisa rela…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

As vezes é necessário configurações e comportamentos adicionais em seus modelos e banco de dados. Caso seja necessário, podemos utilizar o onModelCreating. É um método especial do DbContext que existe pra você configurar qualquer coisa relacionada ao comportamento das suas entidades no EF Core.



Nele dá pra fazer praticamente tudo que envolve mapeamento, configuração, regras do banco, comportamentos extras, definições que o EF Core precisa saber antes de gerar o modelo e muito mais.






Exemplos de uso dentro do OnModelCreating






1. Definir configuração de entidades (Fluent API)






modelBuilder.Entity<Restaurante>().HasKey(x => x.Id);









2. Adicionar dados iniciais (Seed)






modelBuilder.Entity<Restaurante>().HasData(
new Restaurante { Id = 1, Nome = "Picanha Prime" }
);









3. Definir conversão de tipos






modelBuilder.Entity<Pedido>()
.Property(p => p.Status)
.HasConversion<string>();









4. Ignorar propriedades/classes






modelBuilder.Entity<Restaurante>().Ignore(x => x.CalculoInterno);









Boas Práticas (modularizar o arquivo de configuração)



Tudo isso poderia ser feito dentro do método OnModelCreating diretamente, mas na prática profissional jogamos as classes de configurações em arquivos separados e chamamos a configuração pelo arquivo de contexto. Então:




protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
}






E joga as configurações pra classes separadas:




public class RestauranteConfiguration : IEntityTypeConfiguration<Restaurante>
{
public void Configure(EntityTypeBuilder<Restaurante> builder)
{
builder.HasKey(x => x.Id);
...
}
}






Isso mantém o OnModelCreating limpo e organizado.




Data/

├── Context/
└── AppDbContext.cs (OnModelCreating limpo)

├── Configurations/
├── RestauranteConfiguration.cs
├── ReservaConfiguration.cs
└── PedidoConfiguration.cs


Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten onModelCreating

Thematisch verwandte Begriffe: onModelCreating · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-75517 | Novu provides an API for sending notifications through multiple channels…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick