Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot add view to the edmx

When trying to add a view to the edmx file, nothing happens.
I opened the edmx file using wxl editor and I noticed the following error:

warning 6013: The table/view 'CellularOrders.dbo.V_LINK' 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.

(importent thing - I didn't and don't need to add the table which the view based on to the edmx. Moreover, the view is only for doing select statements on the data)

So in the db, I updated the T_LINK table and made one of the fields that reflects on the view as primary key. And then, when I tryed again to add the view to the edmx nothing happens again.

How can I solve this?? Is there an option to fix this without doing anything to the table? Can I add another view that will somehow wrap the old view but with fixed properties?

like image 848
Naor Avatar asked Jun 06 '11 11:06

Naor


People also ask

How do I add a view in Entity Framework?

Let's take a look, how to add views into the model from the database. Step 1 − Create a new Console Application project. Step 2 − Right-click on project in solution explorer and select Add → New Item. Step 3 − Select ADO.NET Entity Data Model from the middle pane and enter name ViewModel in the Name field.

How do I manually edit EDMX?

Right-click on your EDMX file, select Open With…, then select XML (Text) Editor. Boom, there is your entire EDMX file as Visual Studio and . Net sees it, in all its XML wonderfulness.

How do I manually add a table in EDMX?

Open edmx file, right click anywhere in the modal and select "Update Model from Database" and follow the instructions and choose the desired tables and SPs.


2 Answers

Just add a column to your view I added a Row_Number to create a key like this

SELECT ISNULL(CAST((row_number() OVER (ORDER BY tab.ENTRYDATE)) AS int), 0)  AS EDMXID,...other columns go on 

the tab expression is table alias and the entrydate is just a field needed for row_number built in sql-server func.

you may choose diffrent ways, e.g.

select newid() as MYEDMXID,....so on 

Hope Helps

like image 89
Davut Gürbüz Avatar answered Oct 04 '22 08:10

Davut Gürbüz


Each table or view added to entity model must have some key. It actually doesn't have to be primary key. If the table doesn't have the primary key defined EF will try to infer a key using simple rule: It will take all non-nullable non-computed non-binary columns and marks them as an entity key. If none such column exist the entity cannot be automatically added and the designer will throw the mentioned warning. Workaround is adding the view manually and selecting the key yourselves but once you do it you cannot use Update from database because it will always overwrite your changes.

Your defined key should be unique otherwise you can have some other problems related to identity map used internally.

like image 31
Ladislav Mrnka Avatar answered Oct 04 '22 07:10

Ladislav Mrnka