Database design for custom objects for SaaS application - postgresql

I’m looking forward to creating a multitenant-based SASS application. I have defined database design like each tenant using different databases (Postgres) with standard objects(tables) like contact, and accounts. So far clean, I can see many SaaS application supports Custom Object(tables), where customer can create their own objects in real-time and required columns. I would want to support the same. Could someone please explain the backend logic behind that? How can we add new tables for custom objects in the database and refresh the DbContext entity at runtime?
Note: I’m aware for custom fields, many choice JSON-type columns in Postgres, it opens ways to add as many custom columns as JSON type in existing tables. But don’t find any recommended way to do custom object support.

EF doesn't really support adding tables at runtime. You can use ADO.NET queries to work with tables whose schemas aren't known at design-time.

Related

Why do I need models.py for a Flask app?

As a webapp novice, I'm not sure if I need to define models.py.
I already have a working Postgres database on Heroku that I've linked up with Postico and pgAdmin. Using these GUIs, it seems I can get and post data, and make structure changes very simply.
Most tutorials seem to glaze over the details and reasoning for having a models.py. Thanks!
Web frameworks typically enforce or encourage a Model-View-Controller (MVC) patterns that structures code such that the database code is kept separate to the presentation layer.
Frameworks like django come with and are more integrated with ORM functionality which is used to implement an MVC framework. The ORM allows you to programatically interact with your database without having to write sql code. It can let you create a schema as well as interact with it by mapping programming classes to tables and objects to rows.
Flask can be distinguished from many other web frameworks, like django, in that it is considered a micro framework. It is light weight and can be extended by adding extensions. If you need the database integration then you can use it with an external ORM tool like sqlalchemy (and optionally flask-sqlalchemy extension). You can then define a sqlalchemy model, for instance, in a file called model.py or schema.py, or any other name you find appropriate.
If you only need to run one or two queries against an existing postgres database and feel you have no need for the use of an ORM, you can simply use flask with the postgres driver and write the sql yourself. It is not mandatory to have a model.
A model/ORM can be beneficial. For example if you want to recreate an integration/test instance of the database, you can instruct the ORM tool to create a new instance of the database on another host by deploying the model. A model also provides a programming abstraction to the database, which in theory should make your code more database independent (well in theory, its hard to achieve this as databases can have subtle differences), and should make your code less tied to a specific database solution. Also, it alleviates the need of writing a language within a language (sql text strings within python code).

Best practices for using built in Membership Provider

I am creating a custom site using the default membership provider, MVC5, EF6.1.1 using Code First.
The question I have is, what is the best way to link the two user tables? Should I have 2 databases? Should I just build my application into the database for the membership provider? And if that is the case, is there an easy way or a method to extract the GUID that is created when a new user registers to map a foreign key to my user table?
I guess I would rather keep the two separate and not have to deal with writing a custom provider, but I can't seem to wrap my head around the most logical way to do this.
Thanks!
Tony

ASP .NET Membership with Entity Framework

How is everyone designing their EF models when using the built in ASP .NET Membership functionality?
I have many entities (blog posts, comments, photos, etc.) which have a user id associated with them. I currently have a User model that maps to the aspnet_User table, but there is lots of sketchy code juggling around both the MembershipUser entity and the User model which I've created.
Does anybody have any clever solutions I may be overlooking to merge the two entities while still using the included membership functionality?
What I have done in this situation is to create a View in SQL Server, which selects from my own User table and joins one or two columns from the ASP.NET tables. I then map my User entity to this View using ToTable() in DbContext.
This works well enough for me; just note that you cannot use an UPDATE statement on a SQL View if it affects columns from more than one table, so the properties from the ASP.NET tables should not be modified via EF (how you enforce this depends on your implementation).

Is there an ORM that allows a "plugin" to extend the database?

So, I've been searching for the answer to this, but I can't find anything
I have an Entity Framework Model (MyModel1) - for now, we'll say this contains a "Users" table
It's part of a big app, that has a references to an "Addresses" project
The addresses project contains an Entity Framework Model (MyModel2), this contains a Users table, and an Addresses table (pointing to the same database.
The main app has a control that edits the user, and in that control it has an "addresses" control which actually exists in the "Addresses" project.
To make this work, the User control passes the User object down to the addresses control, however, as the User that's been passed belongs to MyModel1 and not MyModel2, another User object has to be loaded up, then it can be used.
This isn't ideal as I've had to load up the User twice. Is there a way of say, MyModel2 extending MyModel1, which effectively just adds a relationship to "User". Or is there an ORM that would handle this better? Or even a design pattern that would handle this better?
I discovered Fluent NHibernate which seems to give me a load more control over how the data layer is put together, through some seriously crazy code I was able to extend the entities in a plugin kind of way, very cool
It sounds like today you have a projects that are a mixture of UI, business logic and data access logic.
A better approach would be to put your Data Access layer into a single project separate from the business logic and the UI. Create an EDMX that includes both Users and Addresses and provide a single ObjectContext that can be used to handle the whole process.
Take a look at the Repository pattern too.

Changing database structure at runtime with Entity Framework?

I have to write a solution that uses different databases with different structure from the same code. So, when a user logs to the application I determine to which database he/she is connected to at runtime. The user can create tables and columns at any time and they have to see the change on the fly. The reason that I use one and the same code the information is manipulates the same way for the different databases. How can I accomplish this at runtime? Actually is the Entity Framework a good solution for my problem?
Thanks in advance.
You can do this with EF 4 using a code-first model. That said, I tend to avoid changing DB metadata on the fly, with or without EF. Rather, I'd choose a schema which fits the user's changing needs.