简体   繁体   中英

Entity-Framework Many-To-Many as Table

Sorry, it's been a while since I've worked with vanilla EntityFramework. I've got a few tables that contain many to many relationships.

public partial class Organization
{
    //...
    public virtual ICollection<aspnet_Roles> aspnet_Roles { get; set; }
}

There are connected by an intermediate table in SQL, I would like to delete the roles from this organization.

I'm aware I can remove the entities from the collection, however I need to do this for several other many to many references, so I'd rather not load everything into memory just to delete it from the collection.

When I'm working on my projects at home, I just add a fake property to the table so the EDMX generates everything as a table, but I doubt I'll be authorized to do that at work.

I would also not like to run raw SQL, or convert everything to use Code First.

Is there a simple way I can delete the the references from the collection Without:

  1. Loading everything into memory
  2. Converting to code first
  3. Running raw SQL statement

Your best bet seems to be to create a stored procedure in your EDMX model that takes the root entities' primary key(s) as input parameter(s) and do your delete logic on SQL side. The only downside I see with this solution is that you still have to query the root entity's/entities' Ids to memory but that shouldn't be a big performance killer.

Doing the full delete logic on the SQL side is the best you can get performancewise.

Here is the official documentation that can help you start if you are not familiar with SP's in EDMX. Stored procedure in EF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM