Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT Security NachrichtenIT Security News Daily Summary 2026-09-23(23.09.2026 um 23:55 Uhr)
IT Security NachrichtenIT Security News Hourly Summary 2026-09-24 00h : 7 posts(24.09.2026 um 00:00 Uhr)
IT NachrichtenWindows-Backup Drive Snapshot ist verkauft(24.09.2026 um 00:01 Uhr)
Apple iOS & macOSHow to Always Show Menu Bar on iPad with iPadOS 27(23.09.2026 um 23:38 Uhr)
IT Security NachrichtenIT Security News Daily Summary 2026-09-23(23.09.2026 um 23:55 Uhr)
IT Security NachrichtenIT Security News Hourly Summary 2026-09-24 00h : 7 posts(24.09.2026 um 00:00 Uhr)
IT NachrichtenWindows-Backup Drive Snapshot ist verkauft(24.09.2026 um 00:01 Uhr)
Apple iOS & macOSHow to Always Show Menu Bar on iPad with iPadOS 27(23.09.2026 um 23:38 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Getting Started with MongoDB in EF Core

Entity Framework Core (EF Core) is a popular ORM for .NET, typically used with relational databases like SQL Server or PostgreSQL. However, with the increasing popularity of NoSQL databases like MongoDB, developers often need to integrate…

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

Entity Framework Core (EF Core) is a popular ORM for .NET, typically used with relational databases like SQL Server or PostgreSQL.

However, with the increasing popularity of NoSQL databases like MongoDB, developers often need to integrate these technologies into their applications.

EF Core 8.0 introduces support for MongoDB, making it easier to work with document-oriented data in your .NET projects with your favourite ORM.



In this blog post, I will show you how to get started with MongoDB in EF Core 8.0.




On my website: antondevtips.com I share .NET and Architecture best practices.

Subscribe to become a better developer.

Download the source code for this blog post for free.






Getting Started With MongoDB In EF Core 8.0





Step 1: Set Up MongoDB



We will set up MongoDB in a docker container using docker-compose-yml:




services:
mongodb:
image: mongo:latest
container_name: mongodb
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
volumes:
- ./docker_data/mongodb:/data/db
ports:
- "27017:27017"
restart: always
networks:
- docker-web

networks:
docker-web:
driver: bridge









Step 2: Add MongoDB Provider and Connect to Database



To connect to MongoDB in EF Core 8.0, you need to add the official MongoDB provider package to your project:




dotnet add package MongoDB.EntityFrameworkCore






Next you need to configure a connection string to the MongoDB in appsettings.json:




{
"ConnectionStrings": {
"MongoDb": "mongodb://admin:admin@mongodb:27017"
}
}









Step 3: Create EF Core DbContext



First, let's create a Shipment entity:




public class Shipment
{
public required ObjectId Id { get; set; }
public required string Number { get; set; }
public required string OrderId { get; set; }
public required Address Address { get; set; }
public required string Carrier { get; set; }
public required string ReceiverEmail { get; set; }
public required ShipmentStatus Status { get; set; }
public required List<ShipmentItem> Items { get; set; } = [];
public required DateTime CreatedAt { get; set; }
public required DateTime? UpdatedAt { get; set; }
}






Here a ObjectId represents a document identifier in a MongoDb collection.



When working with MongoDB, you can create a familiar EF Core DbContext, the same way you do when working with SQL databases:




public class EfCoreMongoDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet<Shipment> Shipments { get; init; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Shipment>()
.ToCollection("shipments")
.Property(x => x.Status).HasConversion<string>();
}
}






You need to add all the entities to the model builder and specify the collection names from the MongoDB.

And now you can use the DbSet in the same manner as with an SQL table.



Notice, that you can use Conversion for your entities.

In the code above we use string Conversion to make sure that enums are stored as strings in the database.



Finally, we need to register our DbContext and specify UseMongoDB for it:




var mongoConnectionString = configuration.GetConnectionString("MongoDb")!;

builder.Services.AddDbContext<EfCoreMongoDbContext>(x => x
.EnableSensitiveDataLogging()
.UseMongoDB(mongoConnectionString, "shipping")
);






MongoDB is a NoSQL database that doesn't have a strict schema definition like SQL does, so you don't need to create any migrations.



Now we are ready to execute our first queries in the MongoDB.






Writing Data Into MongoDb with EF Core



Writing data into Mongodb with EF Core doesn't differ from writing data into SQL Server or PostgreSQL.

The real benefit of using EF Core with MongoDB is that you don't know that you are working with a NoSQL database under the hood.



This is beneficial as you can migrate from a SQL database to MongoDB with only a few tweaks if you're using EF Core.

Or get started with MongoDB right away without having to learn a new API.




But be aware that not all MongoDB features are available in EF Core.

You may need to use the MongoDB.Driver directly for some advanced features.




Here is how you can create, update and delete a document in MongoDB with EF Core:




// Create new shipment
var shipment = request.MapToShipment(shipmentNumber);
context.Shipments.Add(shipment);
await context.SaveChangesAsync(cancellationToken);

// Update shipment
shipment.Status = ShipmentStatus.Delivered;
await context.SaveChangesAsync(cancellationToken);

// Delete shipment
context.Shipments.Remove(shipment);
await context.SaveChangesAsync(cancellationToken);






When assigning an Id, you can use the ObjectId factory method:




Id = ObjectId.GenerateNewId();









Reading Data From MongoDb with EF Core



As you can guess, you can use the familiar LINQ methods in EF Core to read data from MongoDB.



Here is how to select single or multiple records:




var shipment = await context.Shipments
.Where(x => x.Number == request.ShipmentNumber)
.FirstOrDefaultAsync(cancellationToken: cancellationToken);

var shipments = await context.Shipments
.Where(x => x.Status == ShipmentStatus.Dispatched)
.ToListAsync(cancellationToken: cancellationToken);






You can also provide a filtering predicate inside a FirstOrDefaultAsync or other similar methods.



Here is how to check if any entity exists or get count of entities:




var shipmentAlreadyExists = await context.Shipments
.Where(s => s.OrderId == request.OrderId)
.AnyAsync(cancellationToken);

var count = await context.Shipments
.CountAsync(x => x.Status == ShipmentStatus.Delivered);









Limitations When Using Entity Framework 8.0 With MongoDB



The following features are not supported in EF 8.0 for MongoDB:



1. Select Projections:

Select projections use the Select() method in a LINQ query to change the structure of the created object.

EF 8 doesn't support such projections.



2. Scalar Aggregations:

EF 8 only supports the following scalar aggregation operations:




  • Count(), CountAsync()

  • LongCount(), LongCountAsync()

  • Any(), AnyAsync() with or without predicates.



3. Transactions:

EF 8 does not support the Entity Framework Core transaction model for MongoDB.



If you need any of these features without restrictions - use MongoDB.Driver directly.



MongoDB is a NoSQL database, it doesn't support the following EF Core features:




  • Migrations

  • Foreign and Alternate Keys

  • Table Splitting and Temporal Tables

  • Spatial Data



For more information, you can read the official MongoDB documentation.






Summary



EF Core 8 allows seamless integration with MongoDB with familiar Entity Framework classes, interfaces and methods.

You can use the DbContext to configure MongoDB collections.

You can use your favourite LINQ methods to perform read operations and the familiar methods for write operations.



For sure, in the next versions of Entity Framework, support for more and more features will be added for MongoDB.




On my website: antondevtips.com I share .NET and Architecture best practices.

Subscribe to become a better developer.

Download the source code for this blog post for free.


IR-PLAYBOOK-RCE
HIGH
SOC Incident Playbook: Remote Code Execution (RCE) Defense
1-Click Detection Engineering: Sigma & YARA Rules
SOC Ready
title: Detect Exploitation - Getting Started with MongoDB in EF Core
id: 93808b3c-a5d2-41d8-9cab-bff8bc5ac1f3
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-24
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-24"
        description = "YARA Signature for "
    strings:
        $str = "Getting Started with MongoDB i" ascii wide
    condition:
        any of them
}
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Getting Started with MongoDB in EF Core

Thematisch verwandte Begriffe: Getting, Started, with, MongoDB · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-96550 | A vulnerability was found in sfturing hosp_order up to 627f426331da8086c…
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 TTP ⏱️ 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