Adding values to “many to many ” relationship in entityframework

后端 未结 1 519
执念已碎
执念已碎 2021-01-25 23:42

I have 3 entities in Sqlserver that I am mapping with the wizard of visual Studio

The three entities are packages categories and packages_categories,where packages_categ

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-26 00:34

    In order to avoid creating new categories you must attach them to the context before you add the new package:

    var _packagesContext=new DBPackages();
    
    categoriesList.ForEach(c => _packagesContext.Categories.Attach(c));
    
    _packagesContext.Add(new Packages {
                             packageSid="Sid blaaa.",
                             PackageName="TestPackage",
                             Categories=categoriesList
                         });
    

    0 讨论(0)
提交回复
热议问题