EF DBFirst, table gets removed from model - entity-framework

Something strange is happening in my EF Database-first EDMX model.
I wanted to update the model after making some changes in the DB.
The update operation removed a table that did not received any changes. That table was present in the model for a long time and never had any problems.
Obviously, I'm getting the Error 11007 : Entity type 'MyTable' is not mapped.
I tried several things:
Close the EDMX without saving, retry: same problem.
Redo "Update Model from DB", check the box in front of the removed table... But it is not getting added to the model (?)
Close Visual Studio, retry: same problem.
Try in another EDMX (I have 5-6 models in the solution, all linked to the same (big) DB) : There the table is not removed (!)
Rename the table in the DB : With the new name, it IS added.
Clean the EDMX's XML from everything that is linked to that table, retry the update: no effect.
What could be causing a particular table to not be added in a particular emdx model file ? This seems like a bug to me.
NB. Before the table is removed, it is listed in the "Refresh" tab of the "Update from DB" Wizard.
VS 2017 15.4.2 (last update done the day before the problem appeared...?)
EDIT - More information :
The Table is named "DocumentStatuses" and formed of two columns.
CREATE TABLE [cov].[DocumentStatuses](
[Id] [int] NOT NULL,
[Label] [varchar](50) NOT NULL,
CONSTRAINT [PK_DocumentStatus] PRIMARY KEY CLUSTERED ( [Id] ASC )
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
The DocumentStatus entity maps the Id column to an enum. It worked fine until this problem appeared (but it continues to work fine for 2 other entity data models).
EDIT 2
I'm going crazy with this bug. It suddenly appeared, in the same edmx, for 5 others tables that are not linked to each other.
I tried to create another EDMX, with another name, and it has the same bug.
I tried to add tables one-by-one, and suddenly, the problems appears. No matter wich table I add, the 5 others that were present in the model simply disappear and I get the Error 11007.
If anyone has the tiniest idea, it is really welcome.
EDIT 3
I created a new solution, added my tables to a new entity data model... and the problems remains. EF REFUSES to map the tables.
Are there some logs that VS creates when we do "Update from database" ?.
Next step i'll try is to create a VM and try in it.

I "solved" the problem by renaming the table in the database.
This is only a workaround but it's all I could come up with.

I had created "separator" tables with names formed with 20 underscores. Like this : schema._________________________
So I had "separators" between my schemas in SSMS. These dummy tables were not mapped of course.
I removed them and... the problem disappeared !
This is a bug and I reported it:
https://github.com/aspnet/EntityFramework6/issues/418

Related

A single Postgresql table is not allowing deletes and foreign keys are not working

We have a Postgresql database table with many tables. All of the tables seem to be functioning perfectly except for 1. In the last day or two it has stopped performing row deletes. When we try something simple like
delete from bad_table where id_foo = 123;
It acts as if it has successfully deleted the row. But when we do
select * from bad_table where id_foo = 123;
the row is still there.
The same type of queries work fine on all the other tables we tried.
In addition, the foreign keys on this table are not working. There is a foreign key constraint on a column that references a different table. There is an id in the "bad_table", but that id does not exist in the referenced table. Again, foreign key constraints appear to be working fine in all other tables, it is just this one. We tried dropping and recreating the foreign key (which seemed to be successful), but it had no effect.
Between my coworkers and myself we probably have 80 years of relational database experience across oracle, sql server, postgres, etc. and none of us has ever seen anything like this. We've been banging our heads against a wall and are now reaching out to the wider world to see if anyone has any ideas of what we could try. Has anyone else ever seen something like this in Postgres?
It turned out that the issue with foreign keys was solved by dropping the foreign key constraint and then immediately adding it again.
The issue with not being able to delete rows was fixed by dropping a trigger than was called on row delete and then immediate recreating the same trigger.
I don't know what happened, but it is acting like a constraint and a trigger on that particular table were corrupted.

EF6.0 won't create associations VS2013 Legacy 2008R2 Database First

Working to create an EF6.0 connected DB with MVC5 in VSS2013. Previously the model worked, having been created with EF5.0, MVC3 and VSS2010.
Changes were made to the database on existing tables using MS SQL Express - adding some foreign integer keys on child tables pointing to a master parent record, and a single hand-entered test data record set was created for testing. Some extra properties were added. A couple of new unrelated tables.
The "Update Model From Database" picked up the table changes, but has refused to pick up these FK associations. ALL navigation was missing.
The model was deleted, recreated in EF6.0. Once again the EDMX file picks up all tables, and older FK associations. New ones are simply ignored.
There is no point adding tables here as examples, this is simple Parent Child FK stuff using ints. Scripted SQL shows FK constraints for old and new ones are comparable. I have dropped the model and recreated several times, with different ConnectionStrings, name changing in case of confusion. No joy.
I am totally confused as to why it can pick up some, but not others. I need some "gotcha" clues. Anyone?
EF6.0.0 MVC5.1 .NET4.5
Update: Non-Functional FK example
ALTER TABLE [dbo].[Cli_Address]
ADD CONSTRAINT [FK_Cli_Address_WebUserProfileX] FOREIGN KEY ([UserProfileFK]) REFERENCES [dbo].[WebUserProfile] ([UserID]);
You are hit by this:
http://social.msdn.microsoft.com/Forums/en-US/0ba27be1-d252-44c0-8e0b-7f129b578a2b/missing-navigation-properties-on-entity?forum=adodotnetentityframework
Caused by this unneeded index:
CREATE UNIQUE NONCLUSTERED INDEX [IX_WebProfile] ON [dbo].[WebUserProfile]
(
[UserID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
Dropping the index, and everything Works
(I think you will have to drop and recreate the fks before you can drop the index)
UPDATE: I have fixed this, so this scenario is no longer broken , see https://entityframework.codeplex.com/workitem/667

Entity Framework puts all fields in primary key for Firebird tables

I am using a Firebird 2.1 database together with VS2010 (.NET 4.0) and am trying to get it to work properly with the entity framework.
The problem is, that when I generate an entity from a database table, the framework detects all columns to be part of the primary key. The table is very simple with two integer columns, one of them being set as primary key.
I even have "#PK_GEN#" set as comment of the primary key column.
In the EF-Editor I cannot modify the primary key property of the store object, and since I will have to deal with nullable columns, that is a problem. I can edit the XML code of the model file, but the changes are non-persistent when updating the model, so that is a show-stopper.
Since I only read about similar problems concerning views not tables, I am obviously doing something wrong, but I can't figure it out.
edit: By the way, I just tested the behavior with VS 2012 and it remains unchanged.
Here's the CREATE script. Since I'm new to Firebird, there might me something wrong here as well, but I really don't think so.
CREATE GENERATOR GEN_TESTTABLE_ID;
CREATE TABLE TESTTABLE (
TESTTABLE_ID INTEGER NOT NULL,
VALUE INTEGER
);
ALTER TABLE TESTTABLE ADD CONSTRAINT PK_TESTTABLE PRIMARY KEY (TESTTABLE_ID);
COMMENT ON COLUMN TESTTABLE.TESTTABLE_ID IS '#PK_GEN#';
SET TERM ^ ;
CREATE OR ALTER TRIGGER TESTTABLE_BI_GEN_ID FOR TESTTABLE
ACTIVE BEFORE INSERT POSITION 0
AS
begin
if ((new.testtable_id is null) or (new.testtable_id = 0) ) then
begin
new.testtable_id = gen_id(gen_testtable_id, 1);
end
end
^
SET TERM ; ^
Problem is, that Firebird 2.1 contains a bug, that results in this. Generate the model using Firebird 2.5 and you'll be fine.
Some references here, here, here.

how to copy derby table

I am using Eclipse, Java and a Derby database. I want to experiment with changing values that rewrite one of the tables in the db. Before starting the change I would like to copy the particular table (not in code) so that I can restore the original data if necessary. Sof ar googling and searching this site hasnt produced an answer. In Eclipse there is an option to export the db but it calls it a connection so I am not usre what would happen.
If you're not sure about how to connect to the database and issue sql statements, you will need to learn about JDBC. This is a good place to start.
If you're asking about the SQL, it's pretty straight forward. You can create a table based on a select statement.
e.g.
create table table2 as select * from table1 with no data;
Derby is a little strange in this area. You must specify the with no data, and the created table will be empty. You can then issue an insert that will populate the new table if you wish.
insert into table2 select * from table1;
The new table will not have indexes. You will need to create them if you want them. It might retain the primary key. You should check that if you're testing against it. If it doesn't retain the primary key, you should create the primary key before inserting data into the table.
In Eclipse there is an option to export the db but it calls it a connection so I am not sure what would happen.
If what Eclipse does isn't clear for you, you can just as well zip your entire database directory (content of DERBY_HOME env. variable) into an archive. The database must not be running while you make the backup.

MySQL Workbench... Populating the fk column

I'm brand new to MySQL Workbench and a have a bit of experience with databases (MS Access). I'm having trouble populating my fk with data. Here's what I have in my db schema:
2 tables Block and Set (Block having a pk Block_ID (type of INT); Set having fk to Block with fk name Set_Block_ID (type of INT).
1 to many relationship created from Block to Set tables linking Block_ID to Set_Block_ID. Relationship created, no problems
I populate the Block table with data. No problems
I then go to populate the Set table with data. I can see all my columns but not the fk. My question is why?
I have created the exact same db in MS Access and my fk is displayed in the linked table and I can populate it while MS Access makes sure referential integrity is enforced. I'm really brand new to Workbench and cant figure out why I cant see and populate my fk column.
Any help is appreciated!
Thanks!! =)
After having digested all the replies to my question (note sarcasm here) I have finally found a workaround way of solving the issue. To recap:
ISSUE:
created a simple 2 table relationship with Workbench with PK and FK (1 .. n relationship)
FK column not visible in Table Edit so not possible to enter any referencing data
SOLUTION:
installed SQLyog and connected to same server
opened same database and redid the simple 1 .. n relationship
FK column visible for editing in SQLyog
likewise, FK column visible for editing in Workbench
As I said, I'm new to this whole thing so I don't know what the problem was in Workbench. I just know it seems to be working fine now.
As you have noticed, the relationship drawing tool does not create actual foreign key constraints.
However, if double-click the referencing table and switch to the foreign-key tab, you can create references and specify the columns involved. This generates and maintains the visual linkage automatically: