locked
How to make the view not to be read only in EF RRS feed

  • Question

  • Hi,

    I am using VS2012, SQL 2008, EF6.1

    I made a view with the first column marked as not null (with the isnull() function), and all other columns are allow null (with the nullif() function), then I create instead of triggers for the insert & update & delete operations for this view.

    Later in VS2012, I tried to add this view into the EF designer, but it reports me that "Warning    1    Error 6002: The table/view ... does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view", even it has already shown that, the first column is the entity key (an key icon shown at the 1st column, in the entity of the designer).

    Sure, I can not use any dbContext.MyView.Add(...), then dbContext.SaveChanges() to insert the data into database (even with the TSQL, all insert/delete/update statement are all passed tested)

    Please can anyone kindly help me out of this ?  I don't want to use the stored procedures for mapping to the view.

    Thanks for your any comments.

    Saturday, May 17, 2014 1:04 PM

Answers

  • Hello,

    >>Please can anyone kindly help me out of this ?  I don't want to use the stored procedures for mapping to the view.

    In Entity Framework, Views are “readonly” due to the DefiningQuery element. Since you do not want to use the stored procedures for mapping to the view, then you need to make Entity Framework treat views as tables by doing some manual modifications in the generated EDMX file as described think article:

    http://smehrozalam.wordpress.com/2009/08/12/entity-framework-creating-a-model-using-views-instead-of-tables/

    It seems to be a bit complex. And another trick is first using tables to define your model and then replace tables in database with views with same names as tables.

    Regards.


    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 Wednesday, June 4, 2014 7:02 AM
    Monday, May 19, 2014 3:08 AM

All replies

  • Hello,

    >>Please can anyone kindly help me out of this ?  I don't want to use the stored procedures for mapping to the view.

    In Entity Framework, Views are “readonly” due to the DefiningQuery element. Since you do not want to use the stored procedures for mapping to the view, then you need to make Entity Framework treat views as tables by doing some manual modifications in the generated EDMX file as described think article:

    http://smehrozalam.wordpress.com/2009/08/12/entity-framework-creating-a-model-using-views-instead-of-tables/

    It seems to be a bit complex. And another trick is first using tables to define your model and then replace tables in database with views with same names as tables.

    Regards.


    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 Wednesday, June 4, 2014 7:02 AM
    Monday, May 19, 2014 3:08 AM
  • Hi, many thanks for the suggestion!

    Sorry for the reply, since I was very busy for the other projects these days.  We have taken the first trick as our solution, it really works for us!

    Wednesday, May 21, 2014 3:25 PM