GithubHelp home page GithubHelp logo

mrsoxandshoes / entities Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 27 KB

Create C# entity and DTO classes for a SQL Server table or view.

License: MIT License

TSQL 37.58% Python 62.42%
c-sharp data-transfer-object entity-framework metadata sql-server-database

entities's Introduction

Entities

Create C# entity and DTO classes for a SQL Server table or view.

Description

This is a quick-and-dirty implementation to create EF entity classes and DTOs from a SQL Server table or view.

SQL Server

To simplify the process, a SQL view is created to provide the attributes of each column in the table or view:

  • Ordered by COLUMN_ID, the first row is 0 which simply includes the descriptive text (if defined) of the database object and its type (table or view).
  • Database, schema, and table names
  • Column index and name, data type attributes
  • Column comment, default value, and check constraint
  • Column attributes such as nullability and identity

Note: The database user must have "VIEW DEFINITION" granted to read the contents of the default and check constraint values for each table.

Python

The script was created using Python 3.9 with the PyODBC package installed.

  1. The script retrieves the column details for the specified table (or view) in the database.
  2. The columns are iterated through once to generate an entity class which is then written out to a file. The entity class contains:
  • Member declarations.
  • A ToDTO() method to create a DTO instance from the entity instance.
  • A ToString() method to provide custom string output (useful for debugging purposes).
  1. The columns are iterated through again to generate a DTO which is also written out to a file.
  • The DTO contains only member declarations.

Usage

python entity.py --server X --database Y --table T --entityNamespace MyProject.Entities --dtoNamespace MyProject.Dtos --entityFolder Entities --dtoFolder DTOs --username U --password P
Parameter Description Default
--drivers List the ODBC drivers available to PyODBC. If given, execution is stopped afterwards.
--provider Specify the ODBC driver to use. ODBC Driver 17 for SQL Server
--server Name of the SQL Server instance.
--database Name of the database.
--username Username to access the database and table/view. If not used, a trusted connection is assumed.
--password Password to the user login. May be prompted if not passed in.
--schema Schema the table/view is stored in. DBO
--table The table or view to create from.
--entityNamespace The C# namespace of the entity object. Entities
--entityFolder The folder to write the entity class to. Current folder
--dtoNamespace The C# namespace of the entity object. Dtos
--dtoFolder The folder to write the DTO class to. Current folder

TODO

  • Implement parsing of the check constraints.

entities's People

Contributors

mrsoxandshoes avatar

Stargazers

Thomas Ryan avatar

Watchers

 avatar

entities's Issues

An Alternative .ToString()

entity.append("\tpublic override string ToString()")

In the current .ToString() implementation, a StringBuilder is created and then the Column Name and the value for that Column are added in pairs to the StringBuilder object. This is a pretty straight forward implementation, although it requires you to do a bit of string formatting to get it to look right.

As a "simpler" alternative it could be implemented this way:

using System.Text.Json;

public override string ToString()
{
    return JsonSerializer.Serialize(this);
}

This method relies on the System.Text.Json library to serialize the DTO into a string, internally formatted as valid JSON, to represent the DTO. This library is available as part of the ASP.NET Core runtime starting in .NET Core 3.0 (September 2019), so no additional NuGet packages are required.

A benefit of this method is that you can run it in reverse by copying the output of .ToString() and using it as the input for a .FromJSON(string json) method:

using System.Text.Json;

public static DTO? FromJSON(string json)
{
    return JsonSerializer.Deserialize<DTO>(json);
}

Which can be quite helpful when creating an instance of the DTO with known values to use for automated testing. Additionally, you don't have to do any of the string formatting yourself and the string representation of the DTO is in JSON which is a well-known and familiar format.

I like this project as I often find myself manually creating DTOs to represent existing tables/views/stored procedures in SQL Server. EF Core does support automatically scaffolding an existing SQL Server database. Although I have had mixed success with it, as it's awkward and temperamental to figure out the correct commands to pass it, especially if your project has multiple database contexts.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.