21

I've created a view in my database which I would like to include in my entity model. However, when I try to update the entity model through VS 2008, a warning message informs me that the TABLE OR VIEW I'm trying to add doesn't have a primary key.

It seems that in order to add a view to the model, this must have a key field! How can I add this view to my model if views are not permitted to have key field, at least in firebird which is the DBMRS Iā€™m using.

Any idea of how to solve this?

Jon Seigel
  • 11,819
  • 8
  • 53
  • 90
cepriego
  • 844
  • 1
  • 6
  • 10
  • Here is a Visual Studio extension that does it all for you, check out this post: [Frustrated by lack of support for SQL-Views in ADO.NET Entity-Framework Designer?](http://blogs.microsoft.co.il/blogs/shimmy/archive/2010/09/03/frustrated-by-lack-of-support-for-sql-views-in-ado-net-entity-framework-designer.aspx) ā€“ Shimmy Weitzhandler Sep 02 '12 at 00:35
  • 2
    This post may be helpful: [http://smehrozalam.wordpress.com/2009/08/12/entity-framework-creating-a-model-using-views-instead-of-tables/](http://smehrozalam.wordpress.com/2009/08/12/entity-framework-creating-a-model-using-views-instead-of-tables/) ā€“ Syed Mehroz Alam Nov 11 '09 at 16:57

2 Answers2

11

There's a great answer to that here: Entity Framework and SQL Server View (see accepted answer: https://stackoverflow.com/a/2715299/53510.)

EF infers a PK for views by combining all non-nullable fields. You can use ISNULL and NULLIF to manipulate the nullability of view columns thereby forcing EF to pick the PK you want.

Community
  • 1
  • 1
Greg Jackman
  • 686
  • 7
  • 9
  • 4
    If you use ISNULL or NULLIF, that makes the fields read-only, and thus the view becomes a read-only view for the most part. That's only useful in some situations ā€“ Erik Funkenbusch Mar 31 '12 at 21:02
0

There is no keys in firebird views. Instead, set one (or more) field as 'not null' with the following command:

update RDB$RELATION_FIELDS set RDB$NULL_FLAG = 1 where (RDB$FIELD_NAME = 'A_FIELD') and (RDB$RELATION_NAME = 'A_VIEW')

Then re-import the database in entity framework.

Eric Bole-Feysot
  • 11,043
  • 4
  • 42
  • 48