changing the database scheme to avoid having multiple database - asp.net-mvc

We have a web portal in our company that is written in asp.net MVC . right now each department have their own database but we want to avoid having multiple database because the database scheme is the same only it has diffrent data inside for each department.like each department has their own projects etc. how the database model should be changed in such a way to avoid having multiple database ?
also we want to share some elments like projects between diffrents department. how it could be done ?

The single database that you have in mind would have to have its table re-designed such that you would be able to tell, for each record in each table, what department the record relates to. Essentially you would need to add a "DeptId" column to each root-level table. By root level table I mean only the parents of foreign key relationships. Eg, if you have an OrderHeader and OrderLines, only the OrderHeader table would need to have this DeptId column. The lines relate to the header, so you won't need to add this column to the lines table as well. Alternatively, if you have a Customer table, such that each customer belongs to only one department, then you would add the DeptId column to this customer table and then you won't need it on the OrderHeader table (since order header should be referencing Customer table)
Those tables/elements that you want to share between databases just would not have the DeptId column added to them.

Related

Can't use my joining table from DbContext

I have database with different tables. But I can't get access to some tables, and I'm curious why? For example, there is Projects table and Users table in database, and I need to get list of all users that are in specific Project by Id. These tables have many to many relationship, so there is joining table with ProjectId and UserId. It's logic to use some Join to get users that I need from table Users with help of this joining table from db. But how can I make Join when my DbContext doesn't have joining table? How can I get access to joining table using DbContext, or maybe there are some different ways to get list of users by project id?
If someone have similar problem, here is a simple way to use all the data from collections in model
var project = _db.Projects.Include(x => x.Tickets).ThenInclude(y => y.Users).SingleOrDefault(p => p.Id == id);
so after that you gonna have your model and all the data from collections that you need
ProjectUser - is joining table, use for link two tables. This tables must have (usually) Collection for access to this many-to-many data.
So you can will have collection of users for this project. And doing filtering by users from your Ticket (example from image).
Yes, exists few different ways - setting with ef fluent, or attribute, or dbcontext. Better to find complete guide for many-to-many setting in your application.

How to join two tables in HTSQL specific to any columns

HTSQL works when two tables are properly linked with foreign key.
For example:
school can be easily joined with department if we have properly defined a foreign key say school_code of department table referencing code of school.
So far I'm not able to find any way to join two tables on some other columns.
Can any one help me how to do this in HTSQL?
The tweak.override extension allows you to define foreign-keys in a config file even if they're not present in the database itself. http://htsql.org/doc/admin/usage.html#extension-reference
tweak.override:
foreign-keys:
- program(school_code) -> school(code)
- program(school_code, part_of_code) -> program

How to mark data as demo data in SQL database

We haave Accounts, Deals, Contacts, Tasks and some other objects in the database. When a new organisation we want to set up some of these objects as "Demo Data" which they can view/edit and delete as they wish.
We also want to give the user the option to delete all demo data so we need to be able to quickly identify it.
Here are two possible ways of doing this:
Have a "IsDemoData" field on all the above objects : This would mean that the field would need to be added if new types of demo data become required. Also, it would increase database size as IsDemoData would be redundant for any record that is not demo data.
Have a DemoDataLookup table with TableName and ID. The ID here would not be a strong foreign key but a theoretical foreign key to a record in the table stated by table name.
Which of these is better and is there a better normalised solution.
As a DBA, I think I'd rather see demo data isolated in a schema named "demo".
This is simple with some SQL database management systems, not so simple with others. In PostgreSQL, for example, you can write all your SQL with unqualified names, and put the "demo" schema first in the schema search path. When your clients no longer want the demo data, just drop the demo schema.

Best way to save/store customer purchase order data?

I have a custom membership provider which I extended - added a couple of fields, first name, last name, adress, zip code and city.
now, these fields reside in the aspnet_Membership table so that I can easily access them when using the static Membership asp.net class.
now, I want to be able so save customer purchase order data (first name, last name, adress, zip code and city) to the database.
should I in my order model/table use a new set of fields - first name, last name, adress, zip code, city or should I create a relationship between my asp_Membershihp table and my Orders table?
Also, If i have dupe data, once a users asks for his account to be removed I wont have any orphan rows in my Orders table if I use the first method.
so, which is best, to have the user data, first name, last name, adress, zipcode, city in only one table and create a relationship between aspnet_Membership table and Orders table OR create the dupe fields in my Orders table with no relationship to the aspnet_Membership table? Pros cons?
Thanks!
/P
In this scenario, i would rather have the relationship.
Also being the data you are storing Orders (i assume at least, from the name :)) i would maintain a separate set of data on the Order, so one would optionally be able to specify different billing/shipping data than it's Identity on the site.
Another valid reason for duplicate at least some data on the Order table is to have all the necessary data relevant to an Order in the table, thus avoiding problems if the Client request his data to be deleted, and maintain the original values for that data on the order if the customer data were to change in time.
If you are able to, though, you should not actually delete User data, but have a field in which you specify if the User isActive or isDeleted.

How to layout tables and relationships for MVC project

So I am working on an MVC project to put to work the studying I have been doing. I am wrestling with the concept of Database Table relationships and foreign keys. I am working on a simple ecommerce site (displays products, shopping cart, user accounts..etc).
I have the following tables to start out with:
1) Products
2) Categories
I setup the Products and Categories tables to have a ProductId and CategoryId respectively. In my MySQL db, I created a FK on the Products Table to relate to the CategoryId field on the Categories table (I am not sure this was correct to begin).
My expectations for the way the database would handle the table relationship: I didn't want the DB to do anything with the products table if I deleted a category out of the Categories table, or vise versa. The only thing would be that the category field in a Product would be blank (or default) if their category was removed.
Finally, do I have to do anything in my entity classes such as in the Products class, add the ProductId to the Category.ProductId?
Eventually, when I Orders and Users to the project, I can see a relationship where each user -> many orders -> each order has many products -> and each product is in one category.
But I am having a hard time understanding how or if I should be setting up a Foreign key relationship in the two current tables of Products and Categories and if so how to setup my entity class in relation to that FK.
Any advice.
It has been my experience (with L2S) that you don't want to specify any relationships between tables. I have simply done the PK, FK logic myself. This keeps the L2S generated objects simple and is most likely the way you worked with them before in SQL. That, at least, is the case for me.

Resources