How do you create a composite key for a View in SQL Server 2008? - sql-server

I need to create a composite key for a view in SQL Server 2008 because I cannot import a view into entity framework without a primary key defined and since Views don't have primary keys I have to create. Right now I am getting this error when I try to import the View into EF.
The table/view 'FanDB.dbo.Quick_View' 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.
I have been able to add the View to EF before but my boss deleted our DB and so I am recreating the DB from scratch. I have just forgotten how I did it:( Thanks for your help, feel free to ask any questions I will be checking back as often as I can and if I figure it out I will post the solution because all the questions on stack overflow either say to edit the XML file(can't do this because I can't add it to the .edmx) or add a primary key column which I can't do because it is a quick view.

You could recreate the view as a table, add a primary key, import it into EF, and then drop table and reinstate the view. This will get your table into EF, but then you have to be careful whenever you update the model again in the future.
Alternatively, you could create a model db, with all the same object names as your actual db, but with views created as tables. At design time, use the model db, and at run time, use the actual db. So long as the view is updateable, the EF runtime won't care that it's actually a view, and not a table.
If you are feeling compulsive, and have a lot a tables, it wouldn't be too hard to auto-magically generate the model db from the actual db.

Related

Why does view become read only when linked to Access?

In SQL Server Management Studio, I created both indexed and regular (non indexed) views. When I updated the data, the base table's data was changed as well. However, when I link my views to Access using VBA or using Access GUI linked tables I need to specify a index. If I do not do so the view will be read only.
Why is non indexed view read only in Access but not in SQL?
I personally found it very difficult to digest the accepted answer and work out what the problem was and how to resolve it.
Why aren't linked views editable?
Essentially, SQL Server views do not have a Primary Key (PK) and, therefore, are considered read-only (non-updateable) by MS Access because MS Access requires a unique column to use for updates.
How to edit the data in a linked view
A UNIQUE INDEX must be created in MS Access after the view has been linked.
For example, if the table is called dbo_table and the PK column in SQL is called id, create a new query and paste the following into it:
CREATE UNIQUE INDEX myPK ON dbo_table (id ASC)
The word myPK is the name of the index (this can be renamed if needed). Execute the query.
The linked view will now be updateable.
The reference documentation for MS Access CREATE INDEX is available here.
Well, first of all, when you use the Access GUI to link to a table, it NEVER prompts you for an index, it prompt you for the primary key. So let’s get that crazy and silly misinformation corrected like REAL fast, shall we?
When you link to a SQL server view, you get this prompt:
That above prompt is not asking you for a index.
The reason why access does this is because it needs a PK to update a linked table, or a linked view.
PROBLEM:
SQL server does not have a PK defined for a view. And SQL server DOES NOT LET YOU SPECIFY a PK for a view. If SQL server had some way to define a PK for a view, then Access could use that information to grab the PK.
However, since views don’t have a PK on sql server, then when linking to such Views YOU THE HUMAN have to choose a PK.
Access linked tables are read only without a PK.
Access linked views are read only without a PK.
However, sql server views do NOT have a PK defined.
Again:
SQL server views do NOT have a PK defined.
If you create the linked view with VBA code, then no GUI prompt appears asking you for the PK. You can tell Access which column is to be “assumed” as the PK by executing a local create index command. This command DOES NOT ACTUALLY CREATE a index. It does not create one server side, and it does NOT create a actual working index on the access client side.
All the create index command does is tell access what PK column to use. So you not actually creating a index in this case.
Again:
SQL server views do NOT have a PK defined.
If SQL server allowed one to create or set a PK for a view, then Access could simply use that information and know what PK to use, and then set it for you.
However (and again)
SQL server does not have a PK defined for a view.
However for the access client to update a linked table, it needs a PK. When linking to a table, Access can simple ask SQL server what column is the PK. However, you cannot ask SQL server
“what is the primary key” of a view.
(however, you can for a table).
So you not creating a index in Access (it does nothing since a index on the client side of access with a linked table, or a linked view is NOT a working index and does NOTHING!!! (except provide a place to tell access what colum is the PK). It is NOT working index nor is it even a real index.
The existence of an index in Access for linked tables/views does not actually create a working index – only that some “extra” information such as if a PK exists in the table is stored when you “create” that index.
Again: Access does not create indexs for linked table. When you execute create index on a linked table A INDEX IS NOT CREATED IN THIS CASE. ONLY INFORMATION ABOUT THE TABLE OR VIEW IS STORED LOCAL IN ACCESS.
Again: you not creating a index.
Again:
If sql server had a feature or option to define a PK for a view, then no doubt Access would also use that information. However, views in sql server don’t have a defined PK, and thus Access can’t use the PK, and if access can’t find a PK, then it assumes the table is read only.
The SQL management studio can often update tables without a PK, but Access has to use ODBC to connect to sql server, and ODBC has FAR LESS features then SSMS when connecting to a database.

Trying to use a composite unique index instead of a composite unique identifier

I am evaluating outsystems and am trying to write a PoC List/CRUD app based on a legacy database table (the structure of which cannot be changed) which does not have a primary key but instead has two text fields that together constitute a unique index. The database is SQL Server 2014.
The table is successfully added in Integration Studio using "Connect to External table or view".
Then when I open the TestApp in Service Studio and go to the data tab I see the entity there, but it's not usable because it has no identifier defined. I have read some online info that I can double click on the entity and go to the Indexes tab. Here I normally would expect to see the index already defined in the database but it's not there. I also expect to the the New and Delete buttons to be enabled so I can create and delete indexes but New is disabled.
Am I correct to assume that OutSystems should have already "seen" the existing composite unique index? If so, am I correct to assume that this index would have sufficed to create a pseudo/virtual identifier for the entity, whereby making the entity system happy for CRUD and List operations? And also why is the New button not enabled for me to even manually create the index?
Can someone help me please in the correct direction?
Thanks
The OutSystems platform only supports single primary keys, so you have to create the CRUD operations by hand.
For external entities, index information is not fetched from the database, and the reason you can't create the index is because OutSystems doesn't control the metadata (i.e. you can't create indexes, create/modify columns, etc).

Entity Framework referential integrity not being enforced

Using EF6, I have two tables in an SQL database and I've used database first to create the EF diagram from the database.
My database is missing a foreign key constraint between the PK of one table and a foreign key in the other.
Rather than add the foreign key constraint into the database I created it as an association in the EF model.
The association looks good in the EF model, it has the correct principal and dependant.
However if I delete a row from the principal table when dependants are present, referential integrity doesn't kick in and the principal row gets deleted.
I know I can just add the relationship into the SQL database and update model from database. But I'd like to understand the scope of functionality of EF and whether it should be possible to add a relationship in the EF model but not in the SQL database and for it to still work?
It seems to me that if the association (relationship) exists in EF but not in MSSQL, then the association is ignored for referential integrity when you delete a row via EF?
However if I delete a row from the principal table when dependents are present, referential integrity doesn't kick in and the principal row gets deleted.
Dependents are present in database, or are present in Context?
If you haven't loaded all the dependent entities in the context, there is no way for EF to delete them. It needs their key for the delete statement. It doesn't even know they exist if they are not in the context.
So, the only way for referential integrity to work would be if EF retrieves all related entities into the context, on delete of principal entity, so it could issue a delete statement for every one of them (even if there are thousands).
I think, though, if there are dependent entities present in context, EF will issue delete statement anyway (even if it expects db to do all the work).
Hope it helps.

Entity Framework 4.2: InsertFunction for Tables without PK

In Entity Framework 4.2, I am trying to insert a value into a table which has no PK. EF gives me the following error:
Unable to update the EntitySet 'ConditionTypesForWebApplications'
because it has a DefiningQuery and no <InsertFunction> element exists
in the <ModificationFunctionMapping> element to support the current
operation.
From the below discussion, I understood that it is possible to tweak that by editing the edmx file but it is not a best case when you are working with DbContext class.
How do you update a table with a foreign key to another table in ADO.Net Entity Model?
Is there any better way of enabling this?
It is same for DbContext with EDMX (DbContext without EDMX doesn't support entities without keys at all). Entity without key is read only unless you manually change EDMX file (be aware that any update model from database will delete your changes unless you buy more advanced tools for EDMX editing) or you will create stored procedure and map it to insert function of the entity.
In short entity should have key because EF should be able to uniquely identify it. Otherwise you can meet other problems in your application.

Can you lazy load when model has associations, but the database does not?

I am working on an old database, that i do not want to touch or modify in any way if possible.
I want to build a new application that uses it's data but the database has no actual relations despite having primary and foreign keys linking tables.
If i import these tables into an Entity Framework model and add the associations manually, will i be able to use things such as lazy loading and linq?
Many thanks,
Kohan
This is definitely possible. Entity Framework simply generates SQL queries containing joins or where clauses that reference columns that you define in your conceptual model as foreign keys. The generated SQL is directly executed by the database.
Primary and foreign keys are only in your database for referential integrity. As a very simple test you can execute a SQL statement directly in your database that joins two related tables that do not have a foreign key relationship. You'll see that the query simply works. Entity Framework does exactly the same when you correctly define the relationships in your model.

Resources