The Sharp Factory
Esc
↑ ↓ to moveEnter to open
Getting Started / Project Setup

Project Setup

Add the package, the connection string and the repository container to the project that uses the generated code.

On this page

Required NuGet Package#

The generated code talks to SQL Server through Microsoft.Data.SqlClient. Add the Microsoft.Data.SqlClient NuGet package to the generated Repository project (the project that contains the Database class and the repositories); for example from its folder:

dotnet add package Microsoft.Data.SqlClient

Connection String#

Before you can interact with the database from your application using The Sharp Factory Framework you will need to register the connection string.

Because multiple models are supported you will need to register one connection string per model in your project.

Since version 4.0, Microsoft.Data.SqlClient encrypts connections by default, which requires a certificate on the server. If your SQL Server has no certificate (common on development and test servers), add Encrypt=False to your application's connection string, for example:

Data Source=localhost;Initial Catalog=AppDb;Integrated Security=True;Encrypt=False

Registering The Connection String#

We recommend registering the connection string(s) early in the execution pipeline.

For example in your Program.cs class; more specifically in your Main method. Or in the configured entry point for your application.

The Database.RegisterModelConnectionString takes 2 parameters:

  1. int modelId: The code generator assigns a unique model id to each model added to the model container. This can easily be accessed from the RepoLookup.ModelId static class.

  2. string connectionString: This is the connection string that your application will use during runtime to access the database.

The Repository Container#

The RepositoryContainer class is the starting point for all data access and manipulation. This class does not keep any state. This means it can be used as a singleton and we recommend you do so. However you are not constraint to do so if you wish otherwise.

The code sample below register a connection string for a model called AppDb.

C#
// Replace SFDemo with the root namespace// specified when you first generated the modelusing SFDemo.Repository.Common;using SFDemo.Repository.Container;namespace ConsoleClient{    class Program    {        static void Main(string[] args)        {            //Database is a static class            //it was generated by The Sharp Factory App            Database.RegisterModelConnectionString(RepoLookup.ModelId.AppDb,             "Your Application's Connection String Goes Here");               //This is your repository container            //Use it to perform operations against the data store            var repository = new RepositoryContainer();        }    }}