Skip to content

Commit 25b6138

Browse files
committed
Minimal Working Code To Implement CRUD operation on firebase
1 parent 1fe9fe7 commit 25b6138

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

content/blog/general/firestoreCrud.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,12 +705,14 @@ var cardSchema = new Dictionary<string, CollectionSchema>
705705
};
706706
```
707707

708-
708+
#### Create Schema for Cards Collection
709709

710710
```cs
711711
await crud.CreateMasterSchemaTypeByCollectionNameIfNotExistAsync("Cards", cardSchema);
712712
```
713713

714+
#### Add Documents with Reference
715+
714716
```cs
715717
var driverId = await crud.AddAsync("Drivers", new Dictionary<string, object>
716718
{
@@ -720,6 +722,8 @@ var driverId = await crud.AddAsync("Drivers", new Dictionary<string, object>
720722
});
721723
```
722724

725+
#### Add Documents with Reference
726+
723727
```cs
724728
var cardId = await crud.AddAsync("Cards", new Dictionary<string, object>
725729
{
@@ -728,3 +732,77 @@ var cardId = await crud.AddAsync("Cards", new Dictionary<string, object>
728732
{ "DriverId", driverId } // auto-converted to DocumentReference based on schema
729733
});
730734
```
735+
736+
<hr/>
737+
738+
## Minimal Working Example
739+
740+
Here is a minimal working example that demonstrates how to use the Firestore CRUD implementation in a .NET application. This example includes connecting to Firestore, defining collection schemas, and performing CRUD operations.
741+
742+
```cs
743+
// --- 1. Connect to Firestore using Service Account (NEW METHOD) ---
744+
Console.WriteLine("Connecting to Firestore...");
745+
746+
string projectId = "brickssimpledatabase";
747+
string credentialPath = "admin.json"; // <-- MAKE SURE THIS FILE IS IN YOUR RUN DIRECTORY
748+
749+
FirestoreDb db;
750+
try
751+
{
752+
// Open the file as a stream
753+
using (var stream = new FileStream(credentialPath, FileMode.Open, FileAccess.Read))
754+
{
755+
// Create the credential from the stream
756+
var credential = GoogleCredential.FromStream(stream);
757+
758+
db = new FirestoreDbBuilder
759+
{
760+
ProjectId = projectId,
761+
Credential = credential,
762+
EmulatorDetection = EmulatorDetection.None
763+
}.Build();
764+
}
765+
766+
Console.WriteLine($"✅ Successfully connected to project '{projectId}'!");
767+
}
768+
catch (Exception ex)
769+
{
770+
Console.WriteLine($"❌ Connection failed: {ex.Message}");
771+
Console.WriteLine("\nMake sure 'serviceAccount.json' is in the correct folder.");
772+
return;
773+
}
774+
775+
DocumentReference docRef = await db.Collection("Demo").AddAsync(new
776+
{
777+
Name = "Item A",
778+
Category = "Electronics",
779+
Price = Convert.ToDouble(99.99m),
780+
Stock = 10,
781+
Description = "Sample item",
782+
ProfilePhotoUrl = "https://example.com/photo.jpg"
783+
});
784+
785+
DocumentSnapshot snap = await db.Collection("Demo").Document(docRef.Id).GetSnapshotAsync();
786+
787+
if (snap.Exists)
788+
{
789+
var data = snap.ToDictionary();
790+
Console.WriteLine(data["Name"]);
791+
}
792+
QuerySnapshot allSnap = await db.Collection("Demo").GetSnapshotAsync();
793+
794+
foreach (var doc in allSnap.Documents)
795+
{
796+
Console.WriteLine($"{doc.Id} => {doc.ToDictionary()["Name"]}");
797+
}
798+
799+
await db.Collection("Demo").Document(docRef.Id).UpdateAsync(new Dictionary<string, object>
800+
{
801+
{ "Price", Convert.ToDouble(129.99m) },
802+
{ "Stock", 5 }
803+
});
804+
805+
Console.WriteLine("Updated");
806+
await db.Collection("Demo").Document(docRef.Id).DeleteAsync();
807+
Console.WriteLine("Deleted");
808+
```

0 commit comments

Comments
 (0)