如何使用模型实体框架4.1将数据从一个表插入到另一个表中 [英] How Do I Insert Data From One Table To Another Table With Model In Entity Framework 4.1

查看:75
本文介绍了如何使用模型实体框架4.1将数据从一个表插入到另一个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨团队,



我有两个表tempUserCityHotelAccess和UserCityHotelAccess。

我必须将tempUserCityHotelAccess中的数据记录插入到UserCityHotelAccess中实体框架。



我的代码是



Hi team,

I have the two table tempUserCityHotelAccess and UserCityHotelAccess.
I have to insert data record from tempUserCityHotelAccess to UserCityHotelAccess through Entity framework.

my code is

int sessionUsrId = SessionVariables.intUSerID;
tempUserCityHotelAccess item = new tempUserCityHotelAccess();
tempUserCityHotelAccess temptable = (from t in db.tempUserCityHotelAccesses where t.IntUserId ==  (sessionUsrId)  select t);

UserCityHotelAccess maintable  = new UserCityHotelAccess();
maintable.IntUserId = sessionUsrId;
maintable.intHotelId = temptable.intHotelId;
maintable.intCityId = temptable.intCityId;
db.UserCityHotelAccesses.Add(maintable);
db.SaveChanges();





在哪里条件我收到错误的铸造。

请任何身体帮助我为了相同,并给我通过实体框架从一个表到另一个表插入记录的代码。



感谢高级。



In Where condition i am getting error for casting.
Please any body help me for the same and give me the code for the inserting record through Entity framework from one table to another table.

Thanks in advanced.

推荐答案

我最好的猜测是,你的查询不会返回 tempUserCityHotelAccess ,而是 IQueriable 或其他类似的数据类型。这是因为您的查询可能会返回多个结果。



如果您可以保证只返回一个对象,则可以使用 .Single(),否则你可以使用 .First()或许 .FirstOrDefault()



这是一个堆栈溢出问题的链接,关于何时使用查找,首先,单个等。实体框架4 Single vs First vs FirstOrDefault [ ^ ] < br $>


My best guess without you telling us exactly what the error is, is that your query does not return a tempUserCityHotelAccess but an IQueriable or other similar data type. This is because you query could potentially return more than one result.

If you can guarantee that only a single object will be returned you can use .Single(), else you could use .First() of perhaps .FirstOrDefault()

Here is a link to a stack overflow question on when to use Find, First, Single etc. Entity Framework 4 Single vs First vs FirstOrDefault[^]

int sessionUsrId = SessionVariables.intUSerID;
tempUserCityHotelAccess item = new tempUserCityHotelAccess();
tempUserCityHotelAccess temptable = (from t in db.tempUserCityHotelAccesses where t.IntUserId == (sessionUsrId) select t).Single();


这篇关于如何使用模型实体框架4.1将数据从一个表插入到另一个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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