locked
Adding one model in one DBContext to another DBContext at runtime RRS feed

  • Question

  • Hi,

    I have multiple DBContext as per the Bounded Context but now i want one model object to be linked to another context object so that i can query and display. Instead of adding that model to the new context is there any other approach where i can add the models dynamically at runtime.

    Friday, March 27, 2015 1:41 PM

Answers

  • No you cannot do it.

    • Proposed as answer by Fred Bao Monday, March 30, 2015 5:32 AM
    • Marked as answer by Fred Bao Tuesday, April 7, 2015 8:46 AM
    Sunday, March 29, 2015 8:26 PM
  • Hello Mrityunjay,

    >>but now i want one model object to be linked to another context object so that i can query and display

    As darnold924 mentions, we cannot link another context to a new model, each context has its specific entities mapped to database tables.

    >> Instead of adding that model to the new context is there any other approach where i can add the models dynamically at runtime.

    Entity Framework provides a code first approach which would be dependent on .cs file, and in .NET we can create these .cs file with its code you wanted at runtime by using system.reflection.emit:

    https://msdn.microsoft.com/en-us/library/system.reflection.emit(v=vs.110).aspx

    However, since all objects would be created at runtime, we have to read/write these objects by using refection, its performance might

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Tuesday, April 7, 2015 8:46 AM
    Monday, March 30, 2015 5:32 AM

All replies

  • No you cannot do it.

    • Proposed as answer by Fred Bao Monday, March 30, 2015 5:32 AM
    • Marked as answer by Fred Bao Tuesday, April 7, 2015 8:46 AM
    Sunday, March 29, 2015 8:26 PM
  • Hello Mrityunjay,

    >>but now i want one model object to be linked to another context object so that i can query and display

    As darnold924 mentions, we cannot link another context to a new model, each context has its specific entities mapped to database tables.

    >> Instead of adding that model to the new context is there any other approach where i can add the models dynamically at runtime.

    Entity Framework provides a code first approach which would be dependent on .cs file, and in .NET we can create these .cs file with its code you wanted at runtime by using system.reflection.emit:

    https://msdn.microsoft.com/en-us/library/system.reflection.emit(v=vs.110).aspx

    However, since all objects would be created at runtime, we have to read/write these objects by using refection, its performance might

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Tuesday, April 7, 2015 8:46 AM
    Monday, March 30, 2015 5:32 AM