Generating Class Object from Postgresql Database ServiceStack.Ormlite - c#

We are in the process of developing a brand new application.
We want to use ASP.NET MVC 5 with ServiceStack.Ormlite.
Also want to use Postgresql database to store relational objects / tables.
Question: Is there a way / tool to generate C# Class Objects from the Postgresql Database which the ORM then can use to perform operations on the tables.
E.G. I have a table called "Person" in the Database.
What I want to do is, using some tool (Need to know which tool) to generate the C# class object so I can use servicestack.ormlite to add a new person to the table.
Then if I add a column to that table, I generate a new class to replace the old one.
Is that possible and which tool will allow me to do that?

OrmLite's primarily a code-first ORM but does have T4 Templates to help with initially generating the code-first POCO's for existing database tables.
Although after generating the POCO's, I'd be maintaining them (i.e. code-first) from that point forward.

Related

Entity framework : how to generate a SQL Server model

I have a SQL Server with multiple databases.
I'm trying to build a "database browser" with Entity Framework 6.4.4 (in C#, ASP.NET MVC 5) that allows users to access any data stored on a particular SQL Server.
The problem is that we already have a huge amount of databases each containing as many tables and columns and it would really be a pain being forced to generate a model for every database by hand.
Therefore, I thought the entity framework would provide a way to generate a sort of "ServerModel", making it easier to handle multiple databases but also have them packed in a single generated model.
I would use such a model like this:
ServerModel sm = new ServerModel("Sql server name");
//list all databases
foreach(var db in sm.Databases) {} // or maybe sm.ToList() ?
//access specific database model and table
sm.MyDatabase.MyTable.ToList()
I couldn't find anyone trying to achieve this with Entity Framework online.
Is Entity Framework made for this?
Or should I start thinking my own solution?
And... does anyone have any?

Is there any way I can automatically generate BLL classes for a particular database table in c# .NET?

I have been writing BLLs for whatever new tables I create in the database and use them in the Data access layer. I was wondering if someone knows if there is an inbuilt option using .NET for generating BLL classes for a table in sql server database.
It depends on what you want on your objects or the behavior you want to give to them.
You already have Entity Framework as a possibility, that already does something very similar to what you describe.
There is also LLBL Gen Pro (not free, but amazing) that probably does everything you want.
You can also make your own code generator tool combining a template engine (like T4), with queries against your master database, perhaps using MicroORM to simplify DB access.

Entity Framework code first around existing database

I have to create some new entities in a new or existing database using Entity Framework that will need to interact with some legacy tables and I'm wondering what the best approach is here. I was hoping to use Code First with migrations.
For example, say I have an existing populated Person table and I need to create a Animal table that will contain a PersonId foreign key to reference the existing people.
As far as I can tell these are my options:
Create a new DBContext (for a new DB) with an DBSet<Animal>, using code first migrations and POCO entity classes. Ran into problems with setting up the foreign key pointing to another DB.
Create a new DBContext (targeting the existing DB) with DBSet<Animal> and create some kind of POCO wrappers around the existing table. I'm not sure if this is possible - I tried something like but when applying the migration EF tried to create the Person table. I assumed this would map to the existing table instead of creating a new one:
[Table("Person")]
public class Person {
Use Database first with the existing DB and create the tables in the existing DB but then I lose out on using POCO and migrations with my new entites.
Are there any better options that I'm missing or should I go ahead with using Database first?
You can use EntityFramework Reverse POCO Code First Generator.
Reverse engineers an existing database and generates Entity Framework
Code First Poco classes, Configuration mappings and DbContext.
This generator was designed to be customisable from the very
beginning, and not fixed and rigid like other generators. Go and play
with the settings in the .tt file, that's what it's there
for.
Here is the link : EntityFramework Reverse POCO Generator

ASP.net MVC : Create MVC 5 Application using EF6 with Code First and Existing Database

I have to create an application in MVC 5 using EF6. I have already created the schema for the database in SQL Server 2012 and now I want to query this in my app.
The workflow that seems fit is Code First with Existing Database and I have tried to follow below resources but they are a little confusing to me as I am a beginner.
Is there a way I can still use my DB schema in SQL server and go ahead with Code First approach using generated data models from DB.
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
http://www.asp.net/mvc/overview/getting-started/introduction/getting-started
Yes you can reverse engineer code first from existing DB schema. Here you can find instructions how do do it. Since you already created your DB schema you can start from point 3. Reverse Engineer Model.
This process will create for you a DbContext, POCO classes for the tables you selected in the wizard and the mappings. You can use it to query your DB.
When your DB schema changes in the future you can either regenerate POCO classes again or simply edit them by hand (if column type changed simply change the property type, if new column was added add new property to your class). Most devs use reverse engineer code first from existing DB only as a starting point when they need to target existing legacy database. After initial creation all future changes in schema are reflected by manually editing the classes that were originally generated by the tool.

create entities for a dynamically created database in SQL Server

I have an application where I create database's dynamically in a SQL Server using Server Management Objects through my application.
I want to use the Entity Framework to access this database when it has been created, is this possible? As I can not generate Entity classes from a database in VS. I do have the structure of the database of course.
So is it possible to create the Entity classes manually and is that a do-able task?
Yes, it's completely possible. You can even manipulate the generated code if you want.
What you might want to take a look is the EDMX XML specification.
In that file you specify the underlying database, views, functions, procedures and the like, as well as the desired objects. Take a look at MSDN in order to have more information.
Paulo is right for EF 1 (+1). For EF 4, I'd suggest using code-only modeling instead.

Categories