使用EntityFramework 3.5插入记录 [英] Insert record using Entityframework 3.5

查看:92
本文介绍了使用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(); 



状态"表中有数据.

但是我在"mdl.AddToCity(ct)"处出现以下错误

一个IEntityChangeTracker的多个实例不能引用一个实体对象".这是一般错误消息.在这里查看详细信息

+引用/?q = An +实体+对象+不能+被+ IEntityChangeTracker的多个实例+引用+ a [ ^ ]


I have got two tables in database

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[^]


这篇关于使用EntityFramework 3.5插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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