Print Page | Close Window

Two entities using the same database table

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1894
Printed Date: 28-Jan-2026 at 12:12pm


Topic: Two entities using the same database table
Posted By: Peter
Subject: Two entities using the same database table
Date Posted: 14-Jun-2010 at 4:06am

Ok, I just hit the wrong button.. lets resume my post.

Hello,
 
I've just started with DevForce and have a question about modelling my entities.
I'm not sure where EF ends and DF begins, so this may be an EF question.
 
I have a classic 'directory-file' situation
On one side the directory tree and on the other the containing files
I'm using one table to store these instances.

DataTable
- ID  Int
- Name  Varchar
- IsFile Bit
- Parent_ID  Int

DirectoryEntity
- ID  Int
- Name  String
- Parent  DirectoryEntity
- Childs Collection of DirectoryEntiry
- Files  Collection of FileEntity

FileEntity
- ID  Int
- Name  String
- Parent DirectoryEntity

Questions:
1. What is the best way to implement this?
2. How do I hide 'IsFile', but, implement it's value in the correct entity class?
3. EF, by default, makes FK fields public, like 'Parent_ID'. Can I freely set it getter and setter to private?

Thank in advance.
Peter




Replies:
Posted By: ting
Date Posted: 15-Jun-2010 at 8:19pm
Yes, this is more of an Entity Framework question, but it is interesting nonetheless.  I'm not the modelling expert, but I suspect that you will need to unify the DirectoryEntity and FileEntity into a single entity and return empty collections when IsFile is true.  You can make the foreign key fields private/protected (not a bad practice) so that everyone goes through the object-oriented methods.


Posted By: Peter
Date Posted: 17-Jun-2010 at 2:18am

Hello ting,

 

Thank for your response.

I also believe it is a EF question.

But, I find it hard to believe that I need to solve this though one entity.

 

I've read so far:

 

I can define two EF entities and alter the mapping properties.

The mapping properties also can hold conditions to filter the query.

Within my own partial class of Directory and File I can set the 'IsFile' variable.

 

I'm still testing this approche  

I’ll  keep you posted.

Peter



Posted By: ting
Date Posted: 22-Jun-2010 at 7:58pm
Hi Peter,
 
Yes, I forgot about that possibility.  That's seems like a reasonable approach.
 
 


Posted By: Peter
Date Posted: 13-Jul-2010 at 7:46am

For anyone who is intressed.

De EF inheritance type TPH is working in this situation, as I described before.
The problem was (still is), that not in all cases the Visual Studio 2010 IDE correctly updated my EDMX file.
When I deleted an, automatic generated assiocation, the XML tag was not deleted in  the EDMX file.
I had to do it by manually.
I've read about this on the internet but I'm not sure if this is a bug or a local problem.
 
 
 



Print Page | Close Window