1

I am using Oracle as the DB, VS2012, and EF 5.0.

I know the table has a PK (Composite Key) and I create a view with

create view v_table_name as select * from table_name

When I try to add the view to EF, it says there is not PK and makes the object read only. I have tried to add a PK to the view but it tells me that the table can have only on primary key.

I used the following

ALTER VIEW V_TABLE_NAME 
ADD CONSTRAINT V_V_TABLE_NAME_PK PRIMARY KEY (DRSY, DRRT, DRKY) DISABLE NOVALIDATE;

Any idea why EF is not recognizing the PK?

j_freyre
  • 4,623
  • 2
  • 30
  • 47

3 Answers3

1

Because EF doesn't infer PKs for views, as far as I know.

You don't actually have to add the PK to the view if there is a PK on table_name.

You just need to tell EF what the key column(s) are.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
0

Try below without NOVALIDATE

ALTER VIEW V_TABLE_NAME 
ADD CONSTRAINT V_V_TABLE_NAME_PK PRIMARY KEY (DRSY, DRRT, DRKY) DISABLE ;

NOVALIDATE Issue

You can do it by following below mentioned link.

adding primary key to Oracle view

I hope this will help to you.

Community
  • 1
  • 1
Sampath
  • 63,341
  • 64
  • 307
  • 441
  • @CraigStuntz yes but with NOVALIDATE key.May be that is the problem.I have updated my post.Will see ? – Sampath Dec 20 '12 at 07:26
0

Make sure that the underlying tables queried in the view have PKs or at least a non-nullable column(s) that you can designate in the view. If you use the solution provided by [sampath]: https://stackoverflow.com/users/1077309/sampath but the columns are nullable EF won't create an object based on the view.

In this case can designate the key in DbContext.

modelBuilder.Entity<EntityName>().HasKey(e => new {e.KeyCol1, e.KeyCol2,     e.KeyCol3});
Community
  • 1
  • 1