Entity Framework: Table-per-Hierarchy on Multiple Columns - c#

I have set up a view on a legacy table containing order items. In my program, I want to distinguish between phone orders, wifi orders, internet orders, and miscellaneous orders. The legacy table doesn't have a discriminator column, however, you can group the orders by their product code string. Is there a way of creating a table-per-hierarchy from this design?
Example:
Phone codes: 30000-01, 30000-02, 30000-05, ...
Wifi codes: 30000-17, 30000-52, ...
etc.
Edit: I still need an order view showing all of these orders for the department listed as well.

You can create a View per type:
create view PhoneOrders as
select *
from orders
where code in ('30000-01', '30000-02',...
EF should be OK with these, as read-only anyway.

Related

C# Entity Framework 6 how to insert many to many only with ids, not with the entire instance

I have two tables, students and files that have a many-to-many relation.
I have a service that contains an updateStudent method that gets the student's
details including fileIds (as List<guid>), and update them in the database.
The method looks something like this:
var student = context.students.find(id)
...
update student details
...
student.files = **context.files.where(f => fileIds.contains(f.id))**
This line of code:
context.files.where(f => fileIds.contains(f.id))
goes to the database and returns all File's data, including the content (byte array), which I don't really need, because I already have the Id's, and I only want to delete from table studentsFiles all the relevant student file ids and insert the new ones. But for that I don't need the file content. It is unnecessary, and can affect performance.
Do you have any alternative way to only update the many to many relation table with only the ids, without having the entire entry instance?
Thanks in advance

How can I save a 3d array in a SQL table

I'm writing code in C#.
It has two type of user: distributors and clients.
Clients can see all products of all distributors and can select them with mentioning the number of each product they need.So they make a list of orders.
Distributors can see lists of orders received from all clients to them. The table should contains "distributor id" and "client id" and "list of ordered products" with their cost and numbers which have ordered.
But I have no idea about saving this data to a single table.i want that each row mentions to a single list of orders from a special client to a special distributor.
Can I have a 3D table or an array or there is a better and more efficient way?
Is it possible to do what you want? Yes.
Should you? No.
I mean, let's say you set up your table. A column for the distributor, a column for the client, and a column for your list of orders (let's use XML for the example) :
DistID ClientID Orders
1 1 <item>A</item><item>B</item><item>C</item>
2 1 <item>D</item><item>C</item>
2 2 <item>E</item><item>A</item><item>F</item><item>G</item>
... so what problems will you run into? First, imagine trying to add or remove an order. You'd have to read the full Orders list, change it, and then write it all back to SQL. That's a lot of overhead, especially if your 'list' has a lot of items or if you're doing a lot of order adds/removes.
Worse, what happens when you need to find anyone that's ordered 'B'? You're going to have to do a full text scan on that column - no index seeks, no optimizations, nothing - just to find the matching items.
Here's my suggestion - take a look at "SQL Normalization" - basically, the guidelines on how to organize SQL data efficiently and effectively. Here are some good starter places:
http://www.studytonight.com/dbms/database-normalization.php
https://www.essentialsql.com/get-ready-to-learn-sql-database-normalization-explained-in-simple-english/
https://www.guru99.com/database-normalization.html
Based on the information you give, it seems you want a data model like this:
Distributor
Id
Name
Client
Id
Name
Product
Id
DistributorId (foreign key)
Order
Id
ProductId (foreign key)
ClientId (foreign key)
Number
That way you can store product orders from clients, and make sure distributors see order of their own products by selecting all orders having productId's that are linked to the selected distributor.

Entity Framework - Search in 3 tables efficiently

in my DB I have 3 tables like below :
I also have an Autocomplete field in my page in which visitors can search by Country, Province or City and as you know after inserting 3 letter, the search should be conducted by each letter that is inserted. now it seems so costly searching through each table in turn and successively. I also know that City is the most searched field. so what should be the best approach for searching in my case? I should mention that I didn't deploy my web app yet and I can change tables and it's relations.
a code sample would be great.
thanks in advance
MA
Here you can do two things
Create a flat(denormalized data) table combining all the three tables and do the searching in that. Also, you may have to create some triggers on this table to sync the flat table data with data of the actual table.
In a second way, you have to search each table conditionally. Like in your case city is most likely to search then,
a. Search each table
b. Transform the results into a common type
c. Merge the results
d. Sort/select from this merged list

Entity Framework / Database - Archiving Foreign Key Records

I've developed an application for a client with the following relationship:
A SalesOrder has a foreign key relationship to a Customer (one-to-one).
The client now wants to be able to delete Customers so that they won't be available for any future (new) SalesOrders, but obviously I would like to retain the historical records for reporting, etc. Likewise, the record may need to be update-able in the future in rare cases, so in "Edit" mode the customer would need to be there (but ALL other deleted Customers would not)
I am looking for input on a pattern to model this, or better recommendations. My thought was to have an "Archived" bit on the Customer, but my problem is that if someone loads up an old SalesOrder I would also need to load that archived record for the DropDownList that Customers populate.
Since I'm using Entity Framework and an EntityDataSource, my guess is that I might get a runtime exception on the SelectedValue bind of the DropDownList also, but I haven't verified this.
Any thoughts or recommendations on where to start?
Thank you.
I believe I have a solution. I've been racking my brain on it all day but I think it's fairly easy. In a quick prototype it appears to do what I need it to:
I added an "Archived" bit to the Customers table (actually to the BusinessEntity table they inherit from). Then for the "Edit Sales Order" page, when querying Customers, I have a where clause which includes the customer by ID.
For example (pseudocode):
SELECT CustomerID, Name FROM Customers WHERE Archived = 0 OR CustomerID = 52
That pulls all the active customers and the one customer for the record that I need. This allows the customer to still be linked to the record for editing AND doesn't generate a runtime exception with the DropDownList binding.
For reporting, I assume I'll just not filter based on the Archived bit since it's all read-only anyway.

Show Child Rows as Columns

It's been a long time since I've done any serious work with relational database, and despite spending the last two hours searching the Internet and taking my best guesses, I just can't work out the necessary SQL Query expression.
I have three related tables:
Players - Contains a list of players
Coaches - Contains a list of coaches
Evaluations - Stores each coach's evaluation of each player as a number.
Each record in the third table therefore includes a field with the PlayerID, CoachID and the rating.
If I have 10 players and 3 coaches, then table 3 should have 30 records in it.
In order to populate these tables, I want to show the coaches names (pulled from the Coaches table) and display them as Columns in a DataGridView that has a Row for each player so that when you enter a value in a player's row under the coach's column, the value get's stored in Table 3 with the appropriate Player and Coach IDs.
I know I've done something like this year's ago in Microsoft Access, but I can't figure out how to build the Query in my Dataset so that I can bind it to the DataGridView.
I tried using the following SQL Query, but it doesn't let me edit the values in the Coach1 and Coach2 columns. I suspect that's because I have a one-many relationship between PlayerList and Evaluations and a one-many relationship between Coaches and Evaluations:
SELECT PlayerList.Number, PlayerList.Name, PlayerList.Year, PlayerList.Height, PlayerList.Notes, PlayerList.AverageSkill, AVG(Evaluations.Skill) AS CoachSkill,
'SELECT Skill FROM Evaluations
WHERE (PlayerID = PlayerList.Number) AND (CoachID = 1)' AS Coach1,
'SELECT Skill FROM Evaluations
WHERE (PlayerID = PlayerList.Number) AND (CoachID = 2)' AS Coach2
FROM Evaluations RIGHT OUTER JOIN
PlayerList ON Evaluations.PlayerID = PlayerList.Number
GROUP BY PlayerList.Number, PlayerList.Name, PlayerList.Year, PlayerList.Height, PlayerList.Notes, PlayerList.AverageSkill
Any help would be appreciated.
Application details: I'm working with C# in Visual Studio Express with a SQL-Server-CE database.
A little more reading leads me to believe that what I want is a PIVOT function which SQL-Server-CE doesn't support.
Thank you.
You could create new DataRows and add columns dynamically. Then add all your created rows to a DataGridView.
This something related to nested Grid, as hierarchical data
go through this link which in detailed explain how to use gridview/datgrids in scenarios like yours.
http://msdn.microsoft.com/en-us/magazine/cc164077.aspx
hope this will resolve your issue.

Categories

Resources