Create code first, many to many, with additional fields in association table

前端 未结 6 1847
星月不相逢
星月不相逢 2020-11-21 10:21

I have this scenario:

public class Member
{
    public int MemberID { get; set; }

    public string FirstName { get; set; }
    public string LastName { get         


        
6条回答
  •  佛祖请我去吃肉
    2020-11-21 10:44

    TLDR; (semi-related to an EF editor bug in EF6/VS2012U5) if you generate the model from DB and you cannot see the attributed m:m table: Delete the two related tables -> Save .edmx -> Generate/add from database -> Save.

    For those who came here wondering how to get a many-to-many relationship with attribute columns to show in the EF .edmx file (as it would currently not show and be treated as a set of navigational properties), AND you generated these classes from your database table (or database-first in MS lingo, I believe.)

    Delete the 2 tables in question (to take the OP example, Member and Comment) in your .edmx and add them again through 'Generate model from database'. (i.e. do not attempt to let Visual Studio update them - delete, save, add, save)

    It will then create a 3rd table in line with what is suggested here.

    This is relevant in cases where a pure many-to-many relationship is added at first, and the attributes are designed in the DB later.

    This was not immediately clear from this thread/Googling. So just putting it out there as this is link #1 on Google looking for the issue but coming from the DB side first.

提交回复
热议问题