Adding a many-to-many view in EF6 - sql-server

I have an EDMX file which has both tables and views mapped from a SQL Server database. I am trying to add a view named CourseCompany to the EDMX file which serves as a many-to-many connection between two other views.
By looking at another many-to-many association using tables as an example, these are the non-default value properties on the association I added:
Association Set Name: CourseCompany
End1 Multiplicity: * (Collection of Course)
End1 Navigation Property: Companies
End2 Multiplicity: * (Collection of Company)
End 2 Navigation Property: Courses
Name: CourseCompany
But doing this produces this compile error:
Error 3027: No mapping specified for the following EntitySet/AssociationSet - CourseCompany
I made sure the views that this association is going between have their primary keys defined correctly in the EDMX. What do I need to do to add this association in the EDMX?

Follow these steps to resolve this problem:
Add the many-to-many view through the "Update Model from Database" tool.
Edit the newly added entity so that both fields have Entity Key set to true.
Add an Association to the EDMX and configure it as I noted in the question above.
Click on the Association link and go to the Mapping Details tab.
Select the view name in the Maps to dropdown and set the field mappings as well.
Delete the entity that was added in step 1.

This kind of error is often caused by not running the "Generate Database From Model" command (accessible from the context menu of the EDMX designer) after making changes to the design.
Running the tool (if it worked, it will update the SQL generation script) should cause the problem to go away.

Related

Asp.Net core Deleting an entry from table in database: what is the outcome

I have a database with table Photos and table Categories. Each photo is related to one category by categoryId field.
What happens when I delete one category from Categories table? Will the photos with that category be updated with a null value in the categoryID? Or how will entity-framework react to this change?
Another question can I then reset with a mass-change the values of those categories in the photos table? And how can I do that?
hi if you have created relationship between table(using foreign key) then only the deletion of parent table will affect deletion of child table. if you just created table separately and managing relationship with your code then it will not affect the child table. if you are creating using model first approach in entity framework with specifying relation then relationship will be automatically created in backend.
their are four options available in sql on deletion of parent entity
1)No Action
2)Cascade
3)SET NULL
4)SET Default
to know how it will affect check this article
https://www.mssqltips.com/sqlservertip/2365/sql-server-foreign-key-update-and-delete-rules/
That would depend completely on how the ORM that is used defines the database model.
Assuming you use Entity Framework then you can define exactly how EF should react to that situation. In the DbContext you should find a OnModelCreating method in wich you can specify per table what restrictions you want on the table. There you can also define the behaviour of the OnDelete of a foreign key.
If you are not using EF but have your own or a different ORM then again, it depends on how that ORM is configured.
Simple check if you dont know about the used ORM is this: Does the field in the database have a foreign key and how is that configured? Also, is the field categoryID (as defined in the database) nullable? if so, then it apparently doesnt need the relation and shouldnt result in related deletes.

TPT entities derived from TPH base classes in Entity Framework 6?

I have a project that is using EF6 Database first mapped to a SQL database. This is all new so I control the EF model as well as the database schema.
I currently have a table that I'll call Vehicle for simplicity. I use a discriminator column to get subclass Entities Car and Truck. This all works fine.
Now I need to do a 'soft delete' and move any deleted vehicles to a VehicleHistory table. (After trying this w/ EF i will probably use a SQL transaction). This needs to be reviewable so I need this history table mapped as well, but I would like to keep it within the inheritance hierarchy so its easily reused in other classes.
My idea was to create 'vehiclecurrent' and 'vehiclehistory' tables with FK's to Vehicle for shared columns. i would then use TPT in EF to get 'carcurrent','carhistory', ect... derived from my TPH classes(so e.g. carhistory->car->vehicle). This is not working and I get Error 3034: "Entities w/ different keys are mapped to the same row"
So my question is basically how can I pull this off? Will this approach work and how, or is there another way to accomplish this? Thanks!

What do the alternatives for the dialog "Delete Unmapped Tables and Views" do?

I have an entity framework model connected to an SQL Server database. I want to remove a table from the model but not from the database. So I press delete on the table in the model and get a dialog:
Delete Unmapped Tables and Views
The following tables and views in the store model will no longer be mapped. Do you want them deleted?
Yes No Cancel
According to msdn:
Yes: In addition to the objects in the conceptual model that you selected for deletion, all unmapped objects (shown in the dialog box display) are deleted from the storage model. This includes objects in the storage model that were already unmapped, not just objects that have become unmapped as a result of the deleting the selected conceptual model objects.
No: No objects will be deleted from the storage model. Only the objects in the conceptual model that you selected will be deleted.
Cancel: The operation is canceled. No objects in the conceptual model or storage model will be deleted.
So if I understand this correctly, yes and no will both delete the table from the model whereas cancel will not?
Is no also a safe button to press in that it will not drop the table from the database or delete its data?
I am running visual studio 2012, C#, .NET 4.5, and SQL Server 2012
Here is what they really mean:
Yes - delete from the designer and from the sql database.
No - delete from designer
Cancel - does not delete anything
The accepted answer is half correct. Yes does NOT delete the underlying sql table.
No - Deletes from the Diagram but not the .Store storage model:
Yes - Deletes from both the Diagram AND the .Store storage model. NOT the actual Db table.
I do this often when making drastic changes to table structures, i.e. changing column names etc. Choose Yes, then Update Model from DB, and re-add the table.

ADO.net to Lightswitch issue

I've deploy my database through ADO.net Entity Data Model by executing its diagram output query against my pre-made online database.
My database contains Only Lightswitch supported relations, One-Many, ZeroOne-Many & One-ZeroOne.
Finally, Attaching my database to my Lightswitch application as an external database and I was shocked by its result.
Lightswitch converts ALL One-ZeroOne relations to One-Many relations!
I suspect everything, retry tens of times trying to figure this strange issue out with no good news!
Question:
Why Lightswitch converts ALL One-ZeroOne relations to One-Many relations? Inconsistency!
Unless this is a bug, it would depend on the nullablility of your PK/FK. LightSwitch will not allow 0-1 for any key property that is not nullable.
Finally, I've solved it by a brute-force-like trials :S
Solution : ( Please don't ask me a WHY question as IDK will be my answer )
After adding a One-to-ZeroOne Relation you might have one or two Navigation Properties added to associated tables, but
You MUST add a Foreign Key as Scalar Property to the ZeroOne Table with Same type & true Nullable properties.
You MUST adjust the Residential Constrain for the association by double clicking the association line itself and make the FK in the ZeroOne Table dependent on the One table Key.
Generate the database script, execute it and Don't Update LightSwitch data source, Delete data source & Attach it again instead as it won't update relations correctly!

ADO.Net EF - how to define foreign key relation in model first approach?

I have been having previous issues regards to class inheritance and structuring a database around this and using the entity framework to no success. So I have tried to create the entities inside visual studio and see what database tables it creates automatically.
I have a Entity MediaItem which is Abstract and Game Inherits from this. Game has a Console (Int) which corresponds to ConsoleID. However, when I generate the database I get an extra unwanted column (ConsoleTypes_ConsoleID) inside MediaItems_Game table. Why is this and how can I prevent this from happening? Thanks.
First of all your model is probably wrong. ConsoleType and Game is not in one-to-one relation (unless you have single game for each console type). I expect 1 console can have multiple games. So it should be one-to-many. In reality game can be released on multiple platforms so it should be many-to-many.
You got unvanted column because your relation between ConsoleType and Game doesn't know that it should use Console property as a foreign key. This happens if you use independent association. Independent associations are used by default when you draw them from one entity to other entity in Entity designer. You must use foreign key association.
Start with this set up (draw association from ConsoleType to Game - you must have one-to-many relation):
Select relation between ConsoleType and Game an in properties click on Referential Constraint:
In Referential Constraint dialog just set up relation:
Save your model and generate database again.

Resources