Entity framework 如何使用实体框架添加一对多关系
我的web表单项目中有一对多关系。并且总是得到相同的错误:“一个实体对象不能被多个ientitychangetracker实例引用”。 如何解决这个问题,以及如何使用实体框架在一对多关系中添加项?下面是示例代码:Entity framework 如何使用实体框架添加一对多关系,entity-framework,Entity Framework,我的web表单项目中有一对多关系。并且总是得到相同的错误:“一个实体对象不能被多个ientitychangetracker实例引用”。 如何解决这个问题,以及如何使用实体框架在一对多关系中添加项?下面是示例代码: MusicianManager mm = new MusicianManager(); Musician m = new Musician(); m.MusicianName = txtMusicianName.Text;
MusicianManager mm = new MusicianManager();
Musician m = new Musician();
m.MusicianName = txtMusicianName.Text;
m.MusicianSurname = txtMusicianSurname.Text;
m.MusicianInstrument = txtInstrument.Value;
CountryManager cm = new CountryManager();
m.Country = cm.GetById(Convert.ToInt32(drpLstCountry.SelectedValue));
int id = mm.Add(m);
CountryManager类:
using ROCK.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ROCK.DataAccess
{
public class CountryManager
{
public Country GetById(int id)
{
RockolektifDatabaseEntities rde = new RockolektifDatabaseEntities();
return rde.Countries.FirstOrDefault(c => c.CountryId == id);
}
}
}
音乐大师及音乐经理课程:
using ROCK.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ROCK.DataAccess
{
public class MusicianManager
{
public int Add(Musician m)
{
RockolektifDatabaseEntities rde = new RockolektifDatabaseEntities();
rde.Musicians.Add(m);
rde.SaveChanges();
List<Musician> m2 = rde.Musicians.OrderByDescending(mm => mm.MusicianId).Take(1).ToList();
foreach (var item in m2)
{
return item.MusicianId;
}
return -1;
}
}
}
使用ROCK.Entity;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间ROCK.DataAccess
{
公共级音乐家经理
{
公共整数加法(m)
{
RockolektifDatabaseEntities rde=新的RockolektifDatabaseEntities();
rde.音乐家。添加(m);
rde.SaveChanges();
List m2=rde.musiers.OrderByDescending(mm=>mm.MusicianId).Take(1.ToList();
foreach(可变项目,单位为m2)
{
return item.MusicianId;
}
返回-1;
}
}
}
我使用整个设计-(数据访问、实体和用户界面。)您必须了解
Add
方法将触发实体框架执行DetectChanges
操作。此操作将修复所有关系:即,它将调整新音乐家
和国家
的导航属性
通过调用Add
新的music
连接到您在CountryManager
中初始化的DbContext
,同时它还将连接到在MusicianManager
中初始化的DbContext
。要使此操作成功,必须至少禁用其中一个上下文的更改跟踪
如果您需要更多帮助,请向我们展示管理器的构造函数以及GetById
函数的代码
看到更新的代码后,我建议您更改行:
RockolektifDatabaseEntities rde = new RockolektifDatabaseEntities();
要使用块,请执行以下操作:
using(RockolektifDatabaseEntities rde = new RockolektifDatabaseEntities())
{
//the code of the method
return //the entity
}
这将使由两个上下文跟踪的实体成为例外。但可能会出现新的例外情况
在使用块重构到后(您必须这样做,否则您的应用程序将开始泄漏内存),我会将代码重写为:
MusicianManager mm = new MusicianManager();
Musician m = new Musician();
m.MusicianName = txtMusicianName.Text;
m.MusicianSurname = txtMusicianSurname.Text;
m.MusicianInstrument = txtInstrument.Value;
CountryManager cm = new CountryManager();
var country= cm.GetById(Convert.ToInt32(drpLstCountry.SelectedValue));
if(country!=null) m.CountryId=country.CountryId;
int id = mm.Add(m);
这假设music
的导航属性Country有一个名为CountryId
的外键。
请看这篇文章:谢谢。我看到了那个帖子,但我不知道“在服务类之外创建上下文”的含义。所以我打开这个帖子。为什么我必须为课程创建上下文?在你的情况下,你在哪里创建上下文,你对MusicManager和CountyManager有不同的上下文,我认为这就是问题所在。我认为你的经理应该给taje一个上下文作为参数,以确保两者(音乐家和国家)有相同的上下文我编辑了这个问题,我增加了音乐经纪人和乡村经理人课程。