使用EntityFramework 3.5插入记录 [英] Insert record using Entityframework 3.5
问题描述
我在数据库中有两个表
1)状态(stateid,statename)
2)城市(cityid,stateid,cityname)
我正在使用实体框架进行数据库操作
我想使用实体框架将数据引用到State Table插入City表中
为此,我编写了以下代码
MyModel mdl = 新 MyModel();
City ct = 新 City();
ct.cityname = " ;
ct.State = mdl.State.Where(u => u.stateid == 1 );
mdl.AddToCity(ct);
mdl.SaveChanges();
I have got two tables in database 这篇关于使用EntityFramework 3.5插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
状态"表中有数据.
但是我在"mdl.AddToCity(ct)"处出现以下错误
一个IEntityChangeTracker的多个实例不能引用一个实体对象".这是一般错误消息.在这里查看详细信息
1) State (stateid,statename)
2) City(cityid, stateid, cityname)
I am using Entity framework for db manipulation
I want to insert data into City table with the reference to State Table using entity framework
For that i wrote the following code
MyModel mdl= new MyModel();
City ct=new City();
ct.cityname="Dallas";
ct.State=mdl.State.Where(u=>u.stateid==1);
mdl.AddToCity(ct);
mdl.SaveChanges();
There are data in "state" table.
But i am getting following error at "mdl.AddToCity(ct)"
"An entity object cannot be referenced by multiple instances of IEntityChangeTracker"You need to google before posting the question. This is a generic error message. Check the details here
http://www.lmgtfy.com/?q=An+entity+object+cannot+be+referenced+by+multiple+instances+of+IEntityChangeTracker[^]