I have a pretty standard linked table setup
Table1
Id - PK, seeded
Table2
Id - PK, seeded
Table3
Table1Id - FK
Table2Id - FK
For some reason, when using the designer to create the edmx in VS2010, table 1 exists already. When adding Table2 and Table3, they get added, but no relationship gets attached between Table3 and Table1. As well, the designer usually does not include the linking table which is what I would like to continue to do as well. Anyone know why this would be occuring? The existing edmx is quite large and I'd prefer not to delete everything and re-add!
Related
I am using VS2012 and the Entity designer to generate both the database and the models. I have a very basic scenario of Table1 to Table1and2JoinTable to Table2. Something like Students, Classes, StudentClasses. You can have many students in many classes. I would like to have a cascading delete. So if you delete a student any rows in the StudentClass join table are deleted for that student id. Same for deleting a class any rows in the StudentClass are deleted for that class id. After setting up the many to many association in the designer and setting the cascade delete options you get the following error when you attempt to generate the database:
Error 132: End 'Student' on relationship 'Model1.StudentClass' cannot have operation specified since its multiplicity is ''. Operations cannot be specified on ends with multiplicity ''.
Here is a small example:
Here is the association created:
And the resulting error messages:
Here is a portion of the SQL code for generating the database tables:
-- Creating foreign key on [Students_Id] in table 'StudentClass'
ALTER TABLE [dbo].[StudentClass]
ADD CONSTRAINT [FK_StudentClass_Student]
FOREIGN KEY ([Students_Id])
REFERENCES [dbo].[Students]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- This should be ON DELETE CASCADE ON UPDATE NO ACTION;
GO
-- Creating foreign key on [Classes_Id] in table 'StudentClass'
ALTER TABLE [dbo].[StudentClass]
ADD CONSTRAINT [FK_StudentClass_Class]
FOREIGN KEY ([Classes_Id])
REFERENCES [dbo].[Classes]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- This should be ON DELETE CASCADE ON UPDATE NO ACTION;
GO
I know how to work around this by just editing the database script and add in the on delete cascade option. But, I don't want to do this because I am going to come back to the designer many times as the project grows and I don't want to have to remember this step every time.
Has anyone figured out how to resolve this?
It seems to be an edmx restriction, which I don't really understand. Code-first is perfectly capable of generating a junction table with two cascading ON DELETE constraints, but model-first and database-first do not allow the same configuration in the edmx. Normally, cascade actions are configured on the 'one' end of an association. Maybe it is too complex to check the validity of cascade actions on '*' ends (only when both ends are '*').
For the cascaded delete to happen with a context based on an edmx model, you have to load a parent and its children and then delete the parent.
var cls = db.Classes.Include(c => c.Students).Single(c => c.Id = 1);
db.Classes.Remove(cls);
db.SaveChanges();
The executed SQL statements show that the Class is fetched from the database in a JOIN statement with Student. Then the StudentClasss and the Class are deleted respectively.
Obviously, this is much more expensive than enabling cascaded delete in the database.
The work-around to modify the DDL each time after is was generated is not attractive, of course. But I think the only alternative is to make StudentClass part of the model and configure cascaded delete on the 'one' ends of the new associations. Or go code-first.
First of all make sure that you have an ON DELETE CASCADE specified in your Foreign Keys on database side.
I had similar problem and just adding ON DELETE CASCADE solve it instead of setting End1OnDelete and End2OnDelete properties.
I have a junction table with and idenity primary key columns to realize a many to many relationship. Visual Studio automatically detects it as a many to many relationship and the junction table is not an entity.
How can i realize it that also this table is generated as an entity? I need this for breeze.js .
You just need to add additional columns (or properties) to that table (or model).
You said that your table has acolumn named ID and it's the primary key withe IsIdentity set to true. It must works, I'm using this approach...
There must be a problem or missing with your table definition. However, if all are OK, just add a nullable column in your table and update your model from database. The problem will go away.
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:
I need to get the 3rd level entities using navigation properties.
table1
table2
table3
table4
table5
Table 1 relates to table2 and table 2 relates to table 3,4,5 with foreign key relation ships.
I want to pull the data from table 3,4,5 based on the table1 fields. I tried to use the include method but unable to find the method. If you can give the sample code that would be very helpful.
Use the include method in System.Data.Objects. Include should work with the .Include("Table1.PropertyNameTable2.PropertyNameTable3.PropertyNameTable4"); notation.
http://msdn.microsoft.com/en-us/library/bb738708.aspx
Suppose the following database schema:
Table A: AId (PK)
Table B: BId (PK)
Table C: CId (PK)
Table AB: AId, BId (composite PK, FKs to A and B), Data
Table BC: BId, CId (composite PK, FKs to B and C), Data
Table ABC: AId, BId, CId, Data
In the database, ABC has two FKs: one to AB on AId and BId, and one to BC on BId and CId.
Use the EF Designer and attempt to create a Model from this database.
If you have Include foreign key columns in the model checked, it works; but having FK Columns in the model isn't very nice.
If you have Include foreign key columns in the model unchecked, only one of the FKs from ABC will be successfully mapped. To see what went wrong, you have to view the .edmx xml (thanks Craig!) and you see this error:
warning 6037: Foreign key constraint 'FK_ABC_BC' has been omitted from the storage model. Column 'BId' of table 'Model.Store.ABC' is a foreign key participating in multiple relationships. A one-to-one Entity Model will not validate since data inconsistency is possible.
I've read the only other mention of this problem I can find on SO, and I don't think this is the same problem. I can't see anything wrong at a database design level. I'm going to work round this for the time being by imposing surrogate keys on AB and BC, but what I'd really like to know is:
What possible data inconsistency is EF worried about happening here, if it created a model to match the database?
And is there anything I can do to persuade it that everything's going to be OK?
My opinion is that EF is too clever in this scenario and it prevents you from using entity where you can assign only one relation and make the entity non-savable because relation to second entity will not exists.
There is also possibility that EF has some internal problem with tracking state of independent associations if more than one association is based on the same foreign key column but that is just another guess. Generally database features used to map EF features cannot be shared among multiple constructions. The only exceptions I can think about now are primary keys and in their own way discriminator columns.
I would like to mention that I don't like this type of relations in database at all.