Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
A common logic that I see when making object classes is that developers often map classes on a 1:1 assosiation to their DB tables.
For ex, if my DB has 3 tables:

Table_a, Table_b and Table_c,
then, I can create 3 object classes, like Class_a, Class_b and Class_c, with each class having fields corrosponding to one particular table.

Now, from what I read about EF, when you provide a DB to an EF model, it creates the classes and DAL for you, but its mapping logic is not necessarily 1:1 like the above. I think that it somehow combines fields from multiple tables into one class, so that you can end up with fewre number of object classes that the number of tables in your DB, even though all your tables are represented properly and correctly.

Does this mapping logic work by considering tables related by foreign key relationships and combining these into same class, while keeping unrelated tables in other class? Is the foreign key relation the only thing needed to be considered when creating object classes? Or is there some other logic of doing this? Can I manually do what EF does?
Posted

1 solution

Go![^]Go![^]Go![^]
 
Share this answer
 
Comments
princektd 8-Jul-13 6:14am    
Exactly what I was looking fro. Thanks Denis.
Now, I only got to figure out how to do this manually, ie, in C# only, without using EF or XMLs... What i want to do is when manually writing Object classes, I would like to set this up properly, without using EF... I am a new programmer and so am just learning stuff... Any ideas or suggestions are welcome.
And thanx again.
Denis Shemenko 8-Jul-13 6:22am    
You are welcome!
Not sure that you really need hand-written Object classes, because there so many kinds of ORM-s in .Net world, that "bycicle"-inventing really needs in very limited cases.
So if you are beginner, i highly recommend you start with Dapper.

https://code.google.com/p/dapper-dot-net/

Its very simple to use, very fast, and so on.
And when you are "grow up", then you will come for another solutions need, but yourself, with fully understanding of What exactly you want :)
Good luck!
princektd 8-Jul-13 7:10am    
The problem is, though ORM would be helpful, my Team Leader says it needs to be handwritten... Maybe kind of practice/learning thing. He did say that I was welcome to google things out if I was finding difficulty in figuring out things for myself, given that I actually tried to understand what it is that is being done in some code, rather than a copy/paste.

So, i thought i would like to try things out for myself. Thus, I ended up trying to write out object classes in visual studio without using EF, models and the inbuilt dataset/adapter things.

Anyway, I went through your links. The 3rd one shows me how I can write out related objects using 'Virtual' and 'Virtual ICollection'. What i cant understand is, how does the app know if a particular field is a primary key, or which field exactly links 2 objects? I don't see that defined in the example shown there. If these go into some other classes or files, is there any way I can do this in C# in the object class itself? Right now, I am trying to set up an empty project that uses the inbuilt adapter/dataset designer and create a 4 table DB with relations and check to see if i can find how these relations get handled by VS... i have to get things done without the EF or designer tools, so any other ideas?
Denis Shemenko 8-Jul-13 7:33am    
Wow:)
So this is fully your "job" then.
And i have no more idea, except that its only in Your interest to "grow" as developer.
I would have started from DataReader class, but sorry, i've never wrote Database relations in Objects hierarchy by my hands, so doesnt know how can i help you else.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900