none
How to have 2 entities mapped to same table when not conditional? RRS feed

  • Question

  • Hi all,

    I have a table that has a bunch of fields.  I also have a dialog where some number of those fields (about 8 or so) can be entered.  As they enter a value for one of the 8 fields, the app goes out and gets a list of matching elements.  Each time they edit one of the 8 fields, the list is refined. 

    So I thought I would create an entity that contained just those 8 fields (for performance reasons.)  Then I would subclass it to a 2nd entity which had all the remaining fields (lots and lots of them).  This way, I could use the subclass for CRUD and the parent class just for search.  But when I try to do this, it complains (yes, I know why - conditionals, etc.)

    Is there a way for me to configure the parent entity as a read/search only, but subclass it into an entity that has CRUD for all fields (inherited and otherwise)?

    If not, is there a way for me to create 2 separate entities, one which has the 8 searchable fields and the other with all the fields where the first is read/search only and not have the EF complain that there are 2 entities talking to the same table.

    Thanks,

    me

     


    FletcherJ
    Friday, August 12, 2011 10:19 PM

Answers

  • Hello again,

    Thanks for you detailed reply.

    I think you would like to break a table into some parts which included different columns. I suggest you can check this blog about table splitting to the same table. http://blogs.msdn.com/b/adonet/archive/2008/12/05/table-splitting-mapping-multiple-entity-types-to-the-same-table.aspx

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Larcolais Gong Monday, August 22, 2011 11:09 AM
    • Unmarked as answer by Larcolais Gong Tuesday, August 23, 2011 5:21 AM
    • Marked as answer by FletcherJ Tuesday, August 23, 2011 6:24 PM
    Friday, August 19, 2011 2:48 AM
  • Hi all,

    Ok, the approach below is good, it allows you to map one table to 2 entities.  But the more correct answer is to not do it.  If you need only a few fields from the table, you can use something similar to:

    var foo = (from oo in ctx.incidents 
    	 orderby oo.incident_no 
    	 select new { oo.incident_no, oo.date_1, oo.offense }).Take(25);

    This code says to select the first 25 rows, ordered by incident_no and only return the 3 specified fields from the incidents table.  It won't bring down all the fields in each row, just the 3 that are specified.  This is much more efficient than creating 2 (or more) separate entities just to reduce data flow, especially if you will occasionally want all the records....

    HTH,

    me

     

     


    me (and yes, I DO mark correct answers)
    • Marked as answer by FletcherJ Tuesday, September 20, 2011 11:09 PM
    Tuesday, September 20, 2011 11:08 PM

All replies

  • Hello,

    Sorry for the late.

    From your description, I think this question may be solved when you designed your application. Based on my experience, I think you can create some different buttons in your application like InsertData, DeleteData, QueryData. Then, you can set different permissions for different user roles in your application. Just an example, if the administrator logged into application, he should have the full permissions and enable all of buttons in your UI layer. But if a very common user logged into application, he may just has like type-in order into database, so we should just enable InsertData button for this user.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 17, 2011 3:29 AM
  • Larcolais,

    I think you miss-read my question.

    Let's say I have a table with 100 fields, the first 5 of which are ID, name, city, age, dept.  Now I want to have a search page where a user can enter any of those values, in any order.  As they do so, I want to get a result set that includes only those fields.  These values will be displayed somewhere for the user to choose one.  If the user chooses one, then I will use that ID to go back and get all the data.

    What I don't want is to get the data from all 100 fields sent down, just the 5 I need.

    My thought was to create an entity that had only the 5 specified fields.  This could be used for searching, etc.

    Then I would create a subclass of that entity and add in all the other fields (inheriting the 5 that were already defined.  But it won't let me do this because both entities use the same table.

    I was figuring that maybe I could create a readonly entity for the first one and subclass that, but that might result in the subclass entity also having those 5 values as read only.

    Does this clarify the problem?

    Thanks,

    me

     


    FletcherJ
    Thursday, August 18, 2011 10:56 PM
  • Hello again,

    Thanks for you detailed reply.

    I think you would like to break a table into some parts which included different columns. I suggest you can check this blog about table splitting to the same table. http://blogs.msdn.com/b/adonet/archive/2008/12/05/table-splitting-mapping-multiple-entity-types-to-the-same-table.aspx

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Larcolais Gong Monday, August 22, 2011 11:09 AM
    • Unmarked as answer by Larcolais Gong Tuesday, August 23, 2011 5:21 AM
    • Marked as answer by FletcherJ Tuesday, August 23, 2011 6:24 PM
    Friday, August 19, 2011 2:48 AM
  • Larcolais,

    I really appreciate your answer, it was just what I needed.  However, I am frustrated that you went ahead and marked the question answered.  I am very good about following up and marking responses as helpfull and/or answered.  So I find it frustrating when others feel the need to do so for me.  Now, if I had not bother to respond for a few weeks, then sure, go ahead, especially if you know that your response really did answer my question.

    Anyway, I do appreciate the link and the help.

    Me


    FletcherJ
    Monday, August 22, 2011 9:08 PM
  • Hello FletcherJ,

    I'm sorry for this, I re-open this thread and also wait for your feeback.

    If you have any progress, welcome to let us know or further support.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 23, 2011 5:22 AM
  • Larcolais,

    You response was correct and did solve my problem.  And I was happy to mark it as the answer.  I was able to use the information from the link you specified and have created a fully function set of entities as desired.

    Thanks again,

    me


    FletcherJ
    Monday, August 29, 2011 5:30 AM
  • You're welcome. Glad to hear it helps. :) Welcome to come back if you have any question later.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 29, 2011 5:54 AM
  • Hi all,

    Ok, the approach below is good, it allows you to map one table to 2 entities.  But the more correct answer is to not do it.  If you need only a few fields from the table, you can use something similar to:

    var foo = (from oo in ctx.incidents 
    	 orderby oo.incident_no 
    	 select new { oo.incident_no, oo.date_1, oo.offense }).Take(25);

    This code says to select the first 25 rows, ordered by incident_no and only return the 3 specified fields from the incidents table.  It won't bring down all the fields in each row, just the 3 that are specified.  This is much more efficient than creating 2 (or more) separate entities just to reduce data flow, especially if you will occasionally want all the records....

    HTH,

    me

     

     


    me (and yes, I DO mark correct answers)
    • Marked as answer by FletcherJ Tuesday, September 20, 2011 11:09 PM
    Tuesday, September 20, 2011 11:08 PM