3

I need migrate data from transactions saved on an sql server database to a Firestore database. I need do this programatically from a continuous process that will reflect same data from all new transactions on Firestore.

At this moment I can't find any sdk to write data on Firestore with C#.

Any thoughts ?

Dan McGrath
  • 37,828
  • 10
  • 90
  • 123
GCoe
  • 674
  • 1
  • 10
  • 26

1 Answers1

3

There is a nascent C# SDK you could grab; check out Jon Skeet's repo here: http://jskeet.github.io/google-cloud-dotnet/docs/Google.Cloud.Firestore.Data/

(The following is from GitHub)

Installation

Install the Google.Cloud.Firestore.Data package from NuGet. Add it to your project in the normal way (for example by right-clicking on the project in Visual Studio and choosing "Manage NuGet Packages..."). Please ensure you enable pre-release packages(for example, in the Visual Studio NuGet user interface, check the "Include prerelease" box).

Authentication

When running on Google Cloud Platform, no action needs to be taken to authenticate.

Otherwise, the simplest way of authenticating your API calls is to download a service account JSON file then set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to it. The credentials will automatically be used to authenticate. See the Getting Started With Authentication guide for more details.

Getting started

Everything starts with FirestoreDb. Create an instance using the Create or CreateAsync methods, passing in your Google Cloud project ID. The default credentials will be used to authenticate with the server.

From there, you can create, fetch and modify documents, and run queries.

Sample code

FirestoreDb db = FirestoreDb.Create(projectId);

// Create a document with a random ID in the "users" collection.
CollectionReference collection = db.Collection("users");
DocumentReference document = await collection.AddAsync(new { Name = new { First = "Ada", Last = "Lovelace" }, Born = 1815 });

// A DocumentReference doesn't contain the data - it's just a path.
// Let's fetch the current document.
DocumentSnapshot snapshot = await document.SnapshotAsync();

// We can access individual fields by dot-separated path
Console.WriteLine(snapshot.GetField<string>("Name.First"));
Console.WriteLine(snapshot.GetField<string>("Name.Last"));
Console.WriteLine(snapshot.GetField<int>("Born"));

// Or deserialize the whole document into a dictionary
Dictionary<string, object> data = snapshot.ToDictionary();
Dictionary<string, object> name = (Dictionary<string, object>) data["Name"];
Console.WriteLine(name["First"]);
Console.WriteLine(name["Last"]);

// See the "data model" guide for more options for data handling.

// Query the collection for all documents where doc.Born < 1900.
Query query = collection.Where("Born", QueryOperator.LessThan, 1900);
QuerySnapshot querySnapshot = await query.SnapshotAsync();
foreach (DocumentSnapshot queryResult in querySnapshot.Documents)
{
    string firstName = queryResult.GetField<string>("Name.First");
    string lastName = queryResult.GetField<string>("Name.Last");
    int born = queryResult.GetField<int>("Born");
    Console.WriteLine($"{firstName} {lastName}; born {born}");
}
Community
  • 1
  • 1
Dan McGrath
  • 37,828
  • 10
  • 90
  • 123
  • I can't seem to find the repository, any idea where I can find it? I want to submit an issue, the SDK cannot serialize Dictionaries with integers as key. I understand that it uses protobuf for serializatoin, and protobuf can serialize such dictionaries as per this [answer](https://stackoverflow.com/questions/10890922/c-sharp-net-protocol-buffers-protobuf-net-support-for-serializing-dictionary). – AsifM Jan 24 '18 at 14:25