How to retrieve database relationships without EF - c#

I have a very basic question about database programming, here's the problem:
I want to create/read/edit/etc.. data from database without using Entity Framework, and for this job I've chosen SqlFu.
I want to put the stored procedures to create, update, delete on the database and the views to get entities.
My doubt is: If I have an table Employee, that has a one-to-many relationship to Tasks table, when I create a Sql View to retrieve Employee entity, should I retrieve the data in Tasks table that is related to the employee?
If so, how to do that with a single View in SQL Server? If not, I should have different Sql Views that retrieve data from each table and bind the relationship in the application?
I'm a bit lost in this subject :S

No, you don't need. You can retrieve any data from any table/view without need to always retrieve data from any related table's.
On ORM layer it's should be implemented as lazy loading - like in EF. But not in MicroORM's like SQLFu - there you should manually do something like
employeeObject.Tasks = db.Query<Task>("select * from tasks where employeid=#0", employeeObject.Id)
When and if you actually need it.
Yes, you should retrieve data from each table and bind the relationship inside application code.

Related

Dynamic table names with same structure in C# and EF Core

how can I create dynamic table names with the same columns but different table names in C# and EF Core?
For example:
we would like to create tables for collecting emails with the same table structure just different names so each customer has its own table for storing emails. Tables would be generated when the user registers for our service.
EmailTable_{UserGuid}
The reason for this is that each customer can have a collection of 1million+ emails and it would be easier for us to store emails for each customer in its own table for later data analysis, reports, etc.
You're on the right track, but you're not there yet. Give each customer an entire database instead. Then at runtime point the DbContext to the customer-specific database.
What you are proposing will work, sort-of. You would need a separate DbContext subtype for each customer, as OnModelCreating only runs once per DbContext subtype.

Entity Framework 6 dynamically populate data without models

I'm currently trying to read data from a table out of SQL server. The table has 10 columns and when i'm reading the base table by itself, everything works out just fine.
The trouble is, there are X number of extra property tables that may or may not go with my base table. Some databases only have the base table with 10 columns, while others have property tables containing more columns that must be joined into the base table in order to properly display the needed data. Is there a way via EF6 to load that data into a queryable source in a decoupled way?
Basically due to the fact that the extra tables are constantly in flux, I cannot rely on generating models for them and using the mapping EF provides. I do have a model for the base table as its 10 columns never change. I also have a mechanism to read the relational information in order to get the names of the property tables and columns that my program needs to display with the base table when they are available.
Any insight is greatly appreciated.
Good old-fashioned ADO.NET does a good job giving you run-time access to arbitrary query results. You can examine the column data types returned in a DataReader or after loaded into a DataTable, and access columns by name or ordinal position.
And you can interop beteween EF (for your design-time models) and ADO.NET tables that vary at runtime.

Will migrations affect my database?

I am learning Entity Framework to query the database of my company. I have an ASP.NET MVC project and as of now, I have established a connection to the company's principal server database. That has given me access to all the tables and I created a separate class Library containing all the corresponding POCOs(generated automatically).
In the tutorial I was following they say to use "enable-migrations" to have the database updated with the model.
So does that mean that if I were to modify the models in my project, that would have a direct effect on the database? Since I am new to this project I do not want to do anything stupid, like altering the database. For now I just want to query the database and retrieve information, then use that information to show more or less information on a web page.
EDIT: Just as an example, I would like to show a difference between the model generated by EF and what my real table looks like. I have a table Web_Profils that contain and ID, a ProfileName and an Order (int). This DB has no primary keys or foreign keys. If there are relations, they are defined through new tables. But when EF generates all my classes, it adds ICollections, for example in Web_Profils, I have a.o virtual ICollection<Web_User_joint_Profils>Web_User_joint_Profils which is not present in the actual table, it just seems to be the relation that EF has deduced(it is the relation between Users and Profiles present in the table Web_User_joint_Profils). Now, will doing a migration affect my tables just because EF has added these collections in my model?
I've also read that it is possible to deactivate migrations using :
Database.SetInitializer(new ContextInitializerNone<YourDbContext>());
Any thoughts?
If you update your model, you need to add a migration to your project and update your database with that migration.
Unless you do those steps after updating your model, changes will not be reflected in the database.

Create a model from the result of sql statement with entity Framework

In the current environment I am working on, I was confronted with a not very trustful database. Where I have, among all the shannannigans, tables without keys, fields in wrong sizes, very bad separation and relation in tables, and et cettera.
Well, they asked me to make a query in that database, and I can't create a view for such. Nor I have writing access what-so-ever to such DB.
Is it possible to use Entity to parse the result of a query and populate a list of that model?
I know how to do it with nhibernate, and manually, but, is it possible to do it in EF?
Thanks
You simply need to put the SELECT you would have put in your view right into the ExecuteQuery as described in this previous post and do your data validation like you want.

Entity framework - remove related records by id

Okay. assume I have structure:
School -> students -> StudentParents <- parents -> address
School can have many students, students can be relatives and have the same set of parents (may-to-many). Each parent can have multiple addresses.
Assume that students who have the same set of parents cannot study in different schools.
If given school_Id =5, I want to remove this school and all related records.
How to do this easily in Entity Framework 4?
Answer for your question would be same as this question.
You are trying to solve the problem in the wrong layer. You need to
reconsider your database design specially how you maintain the
referential integrity.
You need to set the "CASCADE DELETE"s of the foreign keys and reflect
that in your Entity Model. Then the database will make the necessary
changes to maintain the referential integrity when you delete that
entity.
Entity framework cannot delete data from database that is not instantiated as object in memory. This means you would need to load school data, all students data, all students parent data and so on, and then you would need to manually delete all the data.
This seems like a lot of work to do, so you may want to take another approach to this problem - delete all this data using stored procedure on database that is mapped to ObjectContext, this would perform better since you would not need to get all the data into memory.
But this also seems troublesome. The best approach would be to create Cascade delete constrain on database and map it also in entity framework's model. This has two advantages - you would need to only load school data and after it is deleted from model, it would be deleted from database and cascade delete would remove all referencing data. But if you have school and students data already in memory, EF will take care of marking those objects from memory as deleted, which will make your data consistent with database state.
The best resolution to this problem depends on whether you may or may not modify database. If you can - go for cascade delete. If you cannot - I would recommend stored procedure approach as better performing (assuming performance is an issue and there is lots of students, parents etc. in database).

Categories