none
Cannot add view to Entity Data Model

    Question

  • I have a view that I am trying to add to my ADO.NET Entity Data Model.  Every time I try to Update From Database, and check the view, it refreshes everything else, but does not add the view.  I get no error message or output, so I have no idea what is wrong with the view.  I have read others having an issue with primary key junk once it is added, but I would be pleased as punch just to have it show up, a million keys or not.  Is there a checklist or something that I can check my view against?  Am I missing something, is there a way to turn error messages on?
    Thursday, December 31, 2009 3:50 PM

Answers

  • Is the problem solved? I have the same problem. I can't seem to add the some views. Other views are no problem. Even with a created unique id they don't get added to my EDMX model.

     

    Got it. ! It seems the view must also select a primary key from one of the tables. I tried adding it without a primary key from one of the tables and one with. The last one got added to the .EDMX, so if you create a view, Be sure to select at least one primary key from the tables you select.

    • Marked as answer by BowlerMonk Thursday, April 01, 2010 4:54 PM
    Thursday, April 01, 2010 10:22 AM

All replies

  • I encountered this awhile back, and the only way I could make it actually display the view on the EDMX was by dropping the model and re-creating it.  Luckily, I was in early prototyping and could afford to do this.

    Try this:  create a new view, and just select * from the old one.  If you try to add the new view to your model, what happens?
    My tech blog has EF, C#, and ASP.NET resources
    Thursday, December 31, 2009 5:07 PM
  • Yeah, unfortunately I have tried this half a dozen times, the dropping and re-creating the model.

    I tried wrapping a view around the existing view, and got the same result.  However, I wrapped a view around one of the tables I was using (just a plain select *) and that one showed up.  It leads me to believe there is something wrong with my view, but I can't figure out what.

    Just for testing, I tried adding it to a LINQ-to-SQL class model and it worked there.  I would just use that, but it doesn't play as nice with WCF RIA Services.
    Thursday, December 31, 2009 5:19 PM
  • Hello,

     

    Welcome to ADO.NET Entity Framework and LINQ to Entities forum!

     

    If it is convenient for you, could you please send me a demo project and db file for further investigation, since I cannot reproduce this problem at my lab?   My mail address is v-micsun @ microsoft.com.   Thank you very much!

     

    Happy New Year!

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, January 01, 2010 7:12 AM
    Moderator
  • Hello,

     

    How is the problem now?  If you need any further assistance, please feel free to let me know.


    Have a nice day!
     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, January 07, 2010 12:56 AM
    Moderator
  • Is the problem solved? I have the same problem. I can't seem to add the some views. Other views are no problem. Even with a created unique id they don't get added to my EDMX model.

     

    Got it. ! It seems the view must also select a primary key from one of the tables. I tried adding it without a primary key from one of the tables and one with. The last one got added to the .EDMX, so if you create a view, Be sure to select at least one primary key from the tables you select.

    • Marked as answer by BowlerMonk Thursday, April 01, 2010 4:54 PM
    Thursday, April 01, 2010 10:22 AM
  • I have a view that uses a Recursive Common Table Expression that I needed to be able to consume within Entity Framework, but was having the problem described above. What I did to fix it was add a JOIN that selects a Primary Key field from one of the selected tables to the final SELECT from the Common Table Expression.

    Thanks for this tip! Hopefully this post finds its way to anyone trying to do the same thing I did.


    Microsoft MVP - Windows Live Platform
    Blog: http://pietschsoft.com | Web.Maps.VE - ASP.NET AJAX Bing Maps Server Control
    Sunday, December 12, 2010 4:38 PM
  • View needs a primary key. EF actually creates the view in the edmx, but it comments it out since it can not find a primary key for it. If you know what is the key for the view, uncomment the edmx code and apply key attributes appropriately.

     

    Errors Found During Generation:
       warning 6013: The table/view 'bla' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it.

    Friday, January 07, 2011 4:21 AM
  •  

    I have the same problem here. I have some SQL View in my Sql server Database. I'm trying to add those DB-Views  in my Entity framework and

    every time i've failed  with a error msg. i opened the .EDMX file in XML editor and get the following error as commented.

     <!--Errors Found During Generation:
      warning 6013: The table/view 'BackOfficeBroker.dbo.vw_DefaultClientCommission' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it.
     
      <EntityType Name="vw_DefaultClientCommission">
      <Property Name="BrokerageCommission" Type="decimal" Precision="10" Scale="5"/>
      <Property Name="Margin" Type="decimal" Precision="10" Scale="5" />
      <Property Name="InterestRate" Type="decimal" Precision="10" Scale="5" />
      <Property Name="MinCharge" Type="decimal" Precision="10" Scale="5" />
      <Property Name="ApplyAbove" Type="decimal" Precision="10" Scale="5" />
      </EntityType>
    	 -->
    
    
    

    Searched a lot but couldn't find any solution..... :-|

     

    oh i'm using VS2010 and SQL server 2k8

     

     

     

    Tuesday, January 25, 2011 12:21 PM
  • Yes, I was facing the same issue in case of View. You may need to select Primary key field of selected Tables as output. Entity framework tries to generate PK-FK association that's why it is not able to add view in to entity framework until it has any of the table's PK.
    Wednesday, October 05, 2011 8:42 AM
  • Hi

     I am trying to do the same thing. Adding Views to EF Model. I get not no Errors and I dont find my view in EDMX. IN this answer you have specified to add primary keys to the view.

    In my case my Db Very Old and dont have any primary keys in the tables from which i am creating the views.

    The added some views from the tables which dont have Primarykeys I dont have any problems.

     But For some View created from the some other tables trying to add them I am unable to Add.

    Please let me know If you have any solution to my situation.

    Thanks in Advance.

    Monday, June 11, 2012 11:05 AM
  • So, I added the Key attribute to my view in the edmx file and the next error I got was about all key fields need to be non-nullable. I simply wrapped my key field in "ISNULL" and then updated the model and viola! the view appears....
    Friday, October 26, 2012 10:40 PM
  • The easiest work around is to throw in a getdate() field into the view where the model generator takes it as key and later remove the key and choose your own by altering the model from model view. 

    Please note: You need to have a key, otherwise the entity model does not work.

    Hope this work around helps.

    Monday, December 09, 2013 1:24 AM
  • I was having the same issue, and even when I already had the Key field selected, i kept having same issue on the EDMX model.

    Solution: By creating a new view, I realized that the problem was actually that for some reason after modifying the orignal view on the View Designer on the SQL Management Studio, it added a full FULL OUTER JOIN for some tables, this would imply that there could be values on the key field that could be null, and this was the reason of the problem. Once I got rid of that, I was able to add the view on the EDMX model.

    I hope this helps people for future reference.

     

     
    Tuesday, January 14, 2014 2:34 AM