Entity framework many to many relationship association not automatically generated - entity-framework-6

I've seen this post, my problem is quite the opposite, EF is treating the entities as two separate 1 to many relation, so the User have many UserInRole, and the Role also have many UserInRole entities, shouldn't EF automatically hide the UserInRole table and give a navigation property Roles for User and Users for Role. What I want is actually this:

Deleting the entity from the edmx and updating changes fixed it, apparently when I updated the model from the database in the edmx designer it kept columns that I removed from the database maybe because it was referenced somewhere else in the project, anyway I deleted the entire entity manually and updated the changes / re-added the missing tables and it did the trick.

Related

asp.net mvc load related data from multiple contexts

I have a MVC .Net Core 3.1 project with 2 contexts. One is the standard Identity context and the other is the application context.
These are stored as 2 separate databases on the server.
In my application I have an entity 'Project' and it has a field 'ProjectLeadUserId' in which I store the ID of that user from AspNetUsers.
When I list all of the open projects I would like to show the 'UserName' from AspNetUsers, rather than the GUID related to each of the projects.
Normally I would have a foreign key between the two so that in my view I could do something like:
project.ProjectLead.UserName
But I cant workout how to set that up in my entity as the FK would be in a different context.
Is this possible, or have I caused myself an issue by separating the Identity and application contexts?
Edit:
Ignoring the foreign key issue as I can deal with that in the application logic. Is there a way to load the related data when multiple contexts are involved.
e.g. When I get a list of projects, can I load the related AspNetUsers data, using the AspNetUsers.Id and Project.ProjectLeadUserId?
Quoting from SQL Server documentation:
FOREIGN KEY constraints can reference only tables within the same database on the same server. Cross-database referential integrity must be implemented through triggers. For more information, see CREATE TRIGGER.
It's not possible for you to have a foreign key across different databases in SQL Server.
If the 2 contexts targeted the same DB, I think what you want would still not be possible.
In my past projects I've usually seen people extending the IdentityDbContext to create their DB context and using only that.

Migration conflict in existing Databases - EF6

A developer has enables migrations in a project with an existing database.
Sometimes we get exceptions because the classes don't reflect the tables.
Sometimes there is class properties not mapped to the db, sometimes there isn't even the property but there is a field in DB.
So we have to add some field that allready are are in the database, change another, etc. It causes a lot of changes that couldn't happen.
Currently I'm applying just the insert in migration table to skip the changes, but that isn't the best approach.
How to deal with this situations?
Thanks in advance

Having issues with deleting object graphs

I'm experiencing issues deleting parent objects in an object graph using breeze, when there are complex graphs of children involved. Every time I try to delete a parent, I get foreign key conflicts, even if there is only one simple child. Any advice? Before I post code here, I'd like to understand existing issues I should be aware of. My breeze controller is working with EF6.
The reason you are getting the error is because you have a foreign key constraint. In your code-first DBContext you establish a relationship between a parent and a child and you probably aren't telling EF what to do in case of deletion.
You can either enable the cascading deletes, or set the rules however you want using Fluent API. Check this answer for more details -
Cascade Delete Rule in EF 4.1 Code First when using Shared Primary Key Association

Add existing database view/table from object explorer to entity diagram

I have created a new asp.net 2012 mvc application. I am moving an existing web forms application over to the mvc model. The web forms application uses linq to sql, but since it's no longer going to be supported, I am going to use linq to entities.
Having never used mvc or linq to entities, I have been reading every article I can find.
I added the ado.net entity model and generated the diagram from my existing sql server 2005 database. I was having problems in the .edmx diagram with a relationship so I deleted the view expecting to be able to drag it back to the diagram.
I have looked all over on adding an existing database view/table to a diagram with no luck. I don't want to regenerate it, I spent a lot of time modifying relationships etc. in the current Diagram.
How do I add back a view/table from a database to an existing diagram?
Thank you,
I was afraid the update model from database option would change what I had done in the model, so I avoided it.
After hours of searching, I decided to try it and duh... it let me pick what I want to add.
Right click on your .edmx file and select Update Model From Database. Check what you want and that's it.

Can EF4 generate POCO for me, or do I have to write them myself?

I've been playing about with the Entity 4 framework lately and it's pretty nifty. I've setup a class called Customer.cs with some properties like Name, Address etc. I also have a class called StoreEntities.cs which binds these back to the database through DbSet. It works fine and I can pull all my customers from the database.
The problem is every tutorial I come across on the internet generates their classes by hand. What I mean is, they all say something like "Now I'm going to make a new class called Orders with the following properties" and then proceed to write it out. That might be ok if I was starting a new project, but I'm porting over my old website so I have upwards of 20 tables in my database. If I go through and write all these out by hand I'm going to be there all week :D
Plus I'm not sure what would happen if I made some changes to the database (since I would have to go back through and update all my classes by hand).
I was hoping EF4 would have something similar to a Class Diagram, where I can point it at my database and have it generate a bunch of classes for me based on that.
I'm a little lost on this. Am I going about this the right way?
You should take a look at the ADO.NET C# POCO Entity Generator. With that you should be able to generate your model from your existing database and T4 templates will generate your POCO classes based on your databases tables, etc.
Here is a link to MSDN that explains how to Update Model from Database using the Update Model Wizard.
There are two ways to achieve what you want. Both start from adding new item to your project. In the "Add new item" window select "ADO.NET Entity Data Model". There you will have two options. One to generate your model from existing database and second to create model manualy in designer. If you choose first one you can simply drag and drop tables from server explorer to design surface and all your entities and their relations will be generated for you automaticaly. Also you can modify that generated model later.

Resources