EF 4.3 DB first model not importing associations - sql-server-2005

We moved our DB from an instance of SQL 2005 to a new SQL 2008r2 server. We changed the connection strings in our app to point to new database, even modified the ProviderManifestToken from 2005 to 2008. Everything was working fine until we tried to add entities from our database to our existing edmx model. I'm using the 'update model from database' wizard to add tables to the edmx diagram. The tables will appear in the diagram but only some have their associations and/or foreign keys imported. If I switch the connection string to point back to the old 2005 sql instance everything imports correctly and associations are present. I'm happy to provide additional details if necessary. I can't find a pattern to this at all. What gives?
Update: I created a new project, added an edmx, pointed it to the 2008r2 db instance, selected tables that I know have associations but the designer failed to pick up the associations. Is there something I need to install on the SQL server to get EF to work?

It's working for now. Deleting the connection string from the web.config and then using 'Update Model from Database' and going through the 'Add new connection' dialog to point to the database on the new server seemed to make it behave. If this stops working I'll post back.
Update: Turns out I had two problems. The second was that one of the tables in our DB had two nearly identical redundant indexes on the same column; a clustered PK index and a unique, non-clustered index. So any time I created a foreign key pointing to this table the database chose the unique non-clustered index to enforce the constraint (why? I don't know). But Entity Framework didn't like it and would not create or even recognize the foreign key association. I had to delete the extra non-clustered index and re-create all the foreign keys so they would point to the clustered index. Once that was done I updated my model from the database and all associations were present.
Here's the SQL query I used to check which indexes my foreign keys were using:
SELECT *
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
ORDER BY CONSTRAINT_NAME
The UNIQUE_CONSTRAINT_NAME column for the FK in question should point to a clustered PK for it to work.

How did you move the database to the 2008 Server? Sounds like some or all of the relationships/constraints were not migrated. That would explain why the associations are not showing up when you point to the 2008 database.

Related

DDL changes not showing in Oracle sql developer

I have sql Upgrade script which has many sql statements(DDL,DML). When i ran this upgrade script in SQL developer, it runs successufully.I also provide in my script at the bottom commit. I can see all the changes in the database after running this upgrade script except the unique index constraints. When i insert few duplicate records it says unique constraint violated. It means the table has unique constraints. But i dont know why i cant view this constraints in oracle sql developer. The other DDL changes made i can view.I dont know is there any settings to view it in oracle sql developer.
CREATE UNIQUE INDEX "RATOR_MONITORING"."CAPTURING_UK1" ON "RATOR_MONITORING"."CAPTURING" ("DB_TABLE");
CREATE UNIQUE INDEX "RATOR_MONITORING_CONFIGURATION"."BRAND_UK1" ON "RATOR_MONITORING_CONFIGURATION"."BRAND" ("NAME");
CREATE UNIQUE INDEX "RATOR_MONITORING_CONFIGURATION"."BRAND_BUSINESS_PROCESS_UK1" ON "RATOR_MONITORING_CONFIGURATION"."BRAND_BUSINESS_PROCESS" ("BRAND_ID", "BP_ID");
CREATE UNIQUE INDEX "RATOR_MONITORING_CONFIGURATION"."BRAND_ENGINE_UK1" ON "RATOR_MONITORING_CONFIGURATION"."BRAND_ENGINE" ("BRAND_ID", "ENGINE_ID");
As A Hocevar noted, if you create an index
create unique index test_ux on test(id);
you see it in the Indexes tab of the table properties (not in the Constraints tab).
Please note that COMMIT is not required here, it is done implicitely in each DDL statement. More usual source of problems are stale metadata in SQL Developer, i.e. missing REFRESH (ctrl R on user or table node).
If you want to define the constraint, add following statement, that will reuse the index defined previously
alter table test add constraint test_unique unique(id) using index test_ux;
See further discussion about the option in Documentation
I am assuming you are trying to look for index on a table in the correct tab in sql developer. If you are not able to see the index there, one reason could be that your user (the one with which you are logged in) doesn't have proper rights to see the Index.
If you not obtain any error, the solution is very simple and tedious. SQL Developer doesn't refresh his fetched structures. Kindly push Refresh blue icon (or use Ctrl-R) in Connections view or disconnect and connect again (or restart SQL Developer) to see your changes in structures.

EF5 generates SQL Server CE constraints with dot in name

I am building a .NET disconnected client-server application that uses Entity Framework 5 (EF5) to generate a SQL Server CE 4.0 database from POCOs. The application allows the user to perform a bulk copy of data from the network SQL Server into the client's SQL Server CE database. This is very (VERY) slow, due to the constraints and indexes created by EF5. Temporarily dropping the constraints and indexes will reduce the 30-minute wait to 1 minute or less.
Before starting the bulk copy, the application executes queries to drop the constraints and indexes from the SQL Server CE tables. However, the commands fail, because EF5 created constraint names include the table schema name, dot, and table name. The dot in the constraint name is causing the drop command to fail, due to a parsing issue.
For example, POCO Customer creates table dbo.Customer with the primary key constraint PK_dbo.Customer_Id. The database performs as expected.
However, upon executing non-query:
ALTER TABLE Customer DROP CONSTRAINT PK_dbo.Customer;
SQL Server Compact ADO.NET Data Provider returns an error:
There was an error parsing the query.
[ Token line number = 1, Token line offset = 57, Token in error = . ]
Of course, using a secondary DataContext object that does not have foreign keys generate the database without the constraints, and then add them later works; but, that requires maintaining two DataContext objects and hopefully not forgetting to keep both updated. Therefore, I am looking for one of two solutions:
Compose the DROP statement in such a way that the . character is parsed
Prevent EF5 from using the . character in the constraint and index names
Thank you in advance for your help!
Wrap that bad boy in a []. It tells the parser that everything inside is the key name.
ALTER TABLE Customer DROP CONSTRAINT [PK_dbo.Customer];
Should run fine.
Personally I just wrap every identifier in brackets to avoid this exact issue. So I would write this query like this.
ALTER TABLE [Customer] DROP CONSTRAINT [PK_dbo.Customer];
I think it's more readable that way because you can instantly see identifiers.

Upsizing Access to SQL Server

I use Access 2010 and SQL Server 2005. I am new to the process of "upsizing" which I understand is a legacy term. When I make changes to published tables, I like to localize them back into Access, alter them with the Access interface, and then "re-upsize" them to SQL Server. When I "re-uspize" an altered table Access warns me:
"A table named xxxx already exists. Do you want to overwrite it?"
I choose yes. Then Access reports an error
"Server Error 3726: Could not drop object 'xxxx' because it is
referenced by a FOREIGN KEY constraint."
I understand the importance of foreign key constraints. I have encountered this same trouble using MySQL. In MySQL I would simply set Foreign_Key_Checks = 0; before the import, then set Foreign_Key_Checks = 1; when finished.
Unfortunately in SQL Server, a table cannot be dropped while it's keys are only disabled, they must be deleted. I don't want to delete and recreate foreign keys every time I alter a table. Do I need to start altering my tables in the SQL Server environment? Is there a way to easily "Re-upsize" a table and ignore foreign Key constraints?
If you need to use Access for a front end, instead of keeping an Access DB locally and dealing with the issues of moving back and forth. Try to use Access and connect directly to a version of the sql database you can develop against directly through access. You will probably want to look into using a linked datasource in Access to SQL.
Connecting SQL Server to an Access Database

Best way to remove all tables from a sql 2005 database

I have been developing an asp.net mvc site on a local version of sql server express 2008 and about a month ago I set up the web host and got everything working, the host uses sql server 2005. The site is not live.
Now a month down the track I need to do a fairly big update and the database is way out of date.
I would like to know how I can retain the database but blow everything else away (tables etc.) so I can run the set up script again.
When I try a few things I find on the internet it complains about the foreign keys and doesn't delete.
Thanks in advance
if you truly don't need to retain anything (including users, roles, UDTs, etc), then just drop the db and create a new one.
Take a look at this answer to a question about disabling FK constraints, and rejoice! :)
my guess: it's all about the order you drop the tables
ex
table1
key
fKeyTable2 (foreignkey from table 2)
table2
key
first drop table 2, then drop table1
if its hard to figure out the order you can try this:
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;
(fk_symbol = the foreign key that is being complained about)

Where Used Statements in SQL

I have data used primarily in one table in a database and referenced in several different tables. I need to correct the data. Is it possible to write a "where-used" statement that will search every table in the database and return all tables where the data is referenced?
I am using SQL 2005.
Thanks.
I've found this sql statement here:
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'
AND OBJECT_NAME(OBJECT_ID) LIKE 'FK_%'
NOTE:- I name all foreign key constraints starting with FK_ so it is easy to filter them.
Try adding a diagram to the database, and drop all of the tables onto it. If I've interpreted your question correctly you're trying to understand a database schema that already exists? IF you use the diagram it will draw on the references for you which will allow you to see where the data is linked in your table structure.
As for SQL you can use joins, or where conditions to link data from different tables.
What are you trying to "correct"?
In Management Studio you can right click on a table/view/stored procedure and select View Depenencies. In the dependencies window you can choose to view objects that the selected item depends on or view the items which depend on the selected item.
You can't do it after the fact, but at the table design time you can set up relationships to "ON UPDATE CASCADE".
You could reverse engineer the database (see this posting for a script that does this) if it has foreign keys physically present in the database. If this is not the case then you are up for some manual detective work.
Visio professional has a tool for reverse engineering a database. 2003 doesn't play nicely with SQL Server 2005, so you will need either:
Visio 2007
Modify the script linked to above so it doesn't generate schema references and load the script into a SQL Server 2000 database. Then, reverse engineer from the SQL 2000 DB.
If you don't have foreign keys or have incomplete foreign key coverage, you can look for them manually (for example, look for fields with similar looking codes to reference data) and manually annotate the keys in the diagram. Don't try to do this with the diagramming tool that comes in SSMS as it can attempt to populate the FKs back into the database.