将值添加到“多对多"实体框架中的关系 [英] Adding values to "many to many " relationship in entityframework

查看:64
本文介绍了将值添加到“多对多"实体框架中的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio向导映射的Sqlserver中有3个实体

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

这三个实体是包类别和packages_categories,其中packages_categories具有多对多关系. 映射这些实体后,我得到2个类,分别是Category和Packages,它们看起来像这样: 公共部分课程包 { 公共包() { 类别=新的HashSet(); }

The three entities are packages categories and packages_categories,where packages_categories an many to many relationship. After mapping These entities I am geting 2 classes wich are Categories and Packages,they look like this: public partial class Package { public Package() { this.Categories = new HashSet(); }

    public string PackageSid { get; set; }
    public string PackageName { get; set; }

    public virtual ICollection<Category> Categories { get; set; }
} 

公共局部类类别 { 公共类别 { this.Packages = new HashSet(); }

public partial class Category { public Category() { this.Packages = new HashSet(); }

    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public bool isDefault { get; set; }

    public virtual ICollection<Package> Packages { get; set; }
}

我对多对多关系的实体一无所获(packages_categories之一) 例如,当我尝试从包装中获取信息时,大厅事物就很好用,然后每次我尝试查询一个软件包时,我都会得到它的包装,而当我尝试获取类别的软件包时,事物也是如此.

and I am getting nothing for the entity of the many to many relationship(this one of packages_categories) the hall Thing works great when I try to get info from packages for example then every time i try to query a packages i get ist packages with it and the same Thing when i try to get packages of categories.

问题是当我尝试更新软件包并向其中添加类别时,该类别不仅会添加到entities的packages_categories中,还会添加到类别self中

The Problem is when i try to update a package and add categories to it then the category will be added not just to the entitiy packages_categories but also to categories self

代码示例:

var categoriesList=List<Categoriy>();


categoriesList.Add(new Cateogry{
                    categoryname="catem",
                    IsDefault=false,
                    CategoryId=2332

});

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

现在在此代码中,将具有一个类别的包添加到数据库中,但是如果该类别已经存在,则会抛出一个异常,即该类别已经存在(假设它已经存在,这意味着它将尝试执行以下操作)将其添加到类别"自我),

Now in this code a package with one category will be added to the database,but if the category already there an exception will be thrown that a the category is already there(lets suppose it is already there,this means it tries to add it to Categories self),

我该如何解决这样的问题?我究竟做错了什么 :( 非常感谢.

How can i solve a Problem like this?? what am I doing wrong :( Thanx very much.

推荐答案

为避免创建新类别,您必须在添加新包之前将它们附加到上下文:

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
                     });

这篇关于将值添加到“多对多"实体框架中的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆