EF 6, mapping single entity to many tables - mysql

We have a unique database structure and would like to use the Entity Frame.
Currently we have a large number of tables that contain the same schema. The table name has a reference to the primary key in another table. Take for instance
Table: Customer
Primary Key: CustomerId
Table: User_[CustomerId]
For example the Table Customer has the Primary Key of CustomerId and say has data
CustomerId
1
2
3
4
5
The are multiple User tables (1000's of them) in the database. The table name would be
User_00001
User_00002
User_00003
User_00004
User_00005
each of the user tables have the same columns.
This is something that we cannot change.
We would prefer not to import to import the thousand plus tables into the designer.
Is there a way in the Entity Framework that we could map a single entity to a specific table either in the linq call or in an individual context reference?
For instance something like this?
var myUser = db.User(1020).FirstOrDefault(i=>i.UserId == 134);
or
db.UserID = 1020;
What would the best practice using an ORM be for this?

Related

How to fetch data with many-to-many relations

I am currently designing a database schema. Since I haven't touched SQL for a long time, so I get stuck with fetching data from two tables with many-to-many relation.
I got three tables. Products table stores the information of products, Colors table stores the hex code of colors, and ProductColors represents the many-to-many relation between them. (One product can have many colors, and one color can belong to many products.)
Products
product_id: int PK
product_name: varchar(255)
brand: varchar(255)
Colors
color_id: int PK
color_hex: char(10)
ProductColors
product_id: FK (reference to Products.product_id)
color_id: FK (reference to Colors.color_id)
Suppose I want to find all products that have the color "#68a832" and are from brand "A". I should get "shirt-1" & "shirt-2". What SQL command do I need to execute to retrieve these data? Does it have to use JOIN or there's some method that can retrieve the data without using JOIN.
Thank you!

Relational database and PHP: one-to-many relations with multiple one-tables

Let’s assume there are some rows in a table cars, and each of these rows has an owner. If this owner were always a person (conveniently situated in a table persons), this would be your standard one-to-many relation.
However, what if the owner could not only be a person, but also a company (in a table companies)? How would this relationship be modeled and how would it be handled in PHP?
My first idea was to create a column person and a column company and check that one of them always stays NULL, while the other is filled – however, that seems somewhat inelegant and becomes impractical once there is a higher number of possible related tables.
My current assumption would be to not simply create the foreign key as an integer column person in the table, but to create a further table called tables, which gives IDs to the tables, and then split the foreign key into two integer columns: owner_table, containing the ID of the table (e.g. 0 for persons and 1 for companies), and owner_id, containing the owner ID.
Is this a viable and practical solution or is there some standard design pattern regarding such issues? Is there a name for this type of problem? And are there any PHP frameworks supporting such relations?
EDIT: Found a solution: Such structures are called polymorphic relations, and Laravel supports them.
There are multiple ways to do it.
You can go with two nullable foreign keys: one referencing company and the other user. Then you can have a check constraint which assure you one is null. With PostgreSQL:
CREATE TABLE car{
<your car fields>
company_id INT REFERENCES car,
person_id INT REFERENCES person,
CHECK(company_id IS NULL AND person_id IS NOT NULL
OR company_id IS NOT NULL AND person_id IS NULL)
};
Or you can use table inheritance (beware their limitations)
CREATE TABLE car_owner{
car_owner_id SERIAL
};
CREATE TABLE company{
<company fields>
} INHERITS(car_owner);
CREATE TABLE person{
<person fields>
} INHERITS(car_owner);
CREATE TABLE car{
<car fields>
car_owner_id INT REFERENCES car_owner
};

Get a value going through several tables in entity framework?

I have a situation where I am trying to get a value stored in one table, but have to go through several other tables on the same database to get the correct result.
Table 1 has a PK ID I can query, this PK will give me a new FK in table 2, this will give me a key to table 3 and table 3 will give me key to table 4 that has the value stored against the PK in table 1. If this made sense? I can not do something with the tables or the database, so I need to find a way to select the value in table 4 from the primary key I got in table 1.
Any ideas?
Edit 1 :
I will try to explain better. I have a filepath located in table 4. To get the correct filepath I need to first find the a project id in table 1. That same project id is a FK in table 2. In table 2 I need to find another id (let's call it "customer id") using project id as FK, the customer id is a FK in table 3. In table 3 I need to find purchase id using customer id as FK, the purchase id is a FK in table 4. With the correct FK (purchase id) in table 4, I am able to get the correct filepath. The filepath that coresponds with the project id from table 1.
I am using ASP.NET (Entity Framework) and a SQL database. I was thinking to use Linq, but is somewhat confused about how to do it. Join several tables or try to get the filepath that way?
Did this make it more clear?
use Linq join Query,..
One example, See here
Entity Framework Join 3 Tables
or search LINQ join in Google,you can find the solution yourself.

Designing a database with relational tables in MySQL using Phpmyadmin

I need tips when it comes to designing tables in a database. I am designing an employee meal system that monitors and processes meal logs of employees (like an attendance) My problem is here, I have 2 tables: The employee_table and the log_table. The employee table contains basic employee information and it has a unique key (not a primary one) which is employee_number. And then, there's another table, log_time, which contains the swipe data of the employees. Now, the two tables contain and employee_number column. How do I make relation out of them? Can I bind them together so that when I call for the employee_number column on the log_time it will get the basic information of the employee on the other table as well? Sorry because I'm having a hard time when it comes to designing a database.
SQL syntax for a relation might look something like this:
CREATE TABLE employee_table(
FOREIGN KEY (employee_number) REFERENCES employees(number),
...other stuff...
)
With another table that looks like
CREATE TABLE employees(
number INT(10) NOT NULL
)
Here's a great site on SQL foreign keys:
http://www.sitepoint.com/mysql-foreign-keys-quicker-database-development/

Advice needed on database design

I am new to database designing. In my case I have to generate lot many keys per user per product. So, I have two options -
Create one table with product_id and key for all the users, or
Create a separate table for each user
In the former case I will have a single table but querying might take more time as all the entries are in the same table for all the users.
In the later case queries might return the result faster but more tables and if users cross 100 or more than it means lot of tables.
Definitely do not create a table for each user. if you create a single table for all users you can use relational database design and add specific information pertaining to each user like address or employee information and use the primary key from the users table as a foreign key. and there will not be any noticeable lag. And maintenance will be whole lot easier
if you want to build relation between your user and product then make table like below
user_product [table name]
id [Primary Key]
user_id [Reference key of user table]
product_id [Reference key of product table]
key
This is your table schema You must use.
if you generate each table then this will take more complex for database and relation management. So, just use above row base format.
if that helpful then let me know.
Thanks