如何使用实体框架将数据插入到索引表中 [英] How can I insert data into refrencing table using entity framework

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

问题描述

我有三个桌子. tbl_area,tbl_item和tbl_areaitemmap.
tbl_area包含数据,无需在此表中插入.

tbl_areaitemmap
mapid-主键
fkareaid-tbl_area(areaid)的前键
fitemid-tbl_item(itemid)的前键


我只想当我将数据插入到tbl_areaitemmap中时,如果该项目存在,则还会在tbl_item anf中插入一个,然后tbl_areaitemmap引用那些id.

像这样?????请告诉我..
using(sampleEntities _entitiesContext = new SampleEntities())
{
var context = _entitiesContext.tbl_areaitemmap;

var obj = new tbl_areaitemmap();
obj.tbl_area_r.AreaId = 7;
obj.tbl_item.pkItemId = 12;
obj.status ="T";

_entitiesContext.AddTotbl_areaitemmap(obj);
试试
{
_entitiesContext.SaveChanges();
返回true;
}
catch(Exception)
{

返回false;
}
}

I having three tables. tbl_area, tbl_item and tbl_areaitemmap.
tbl_area have data and no need to insertion in this table.

tbl_areaitemmap
mapid - primary key
fkareaid - foriegnkey to tbl_area(areaid)
fitemid - foriegnkey to tbl_item(itemid)


I just want when I''m insert data into into tbl_areaitemmap there is also a insertion in tbl_item anf if the item is exist then tbl_areaitemmap refrence to those id.

just like this????? plz tell me..
using(sampleEntities _entitiesContext = new SampleEntities())
{
var context = _entitiesContext.tbl_areaitemmap;

var obj = new tbl_areaitemmap();
obj.tbl_area_r.AreaId =7;
obj.tbl_item.pkItemId = 12;
obj.status = "T";

_entitiesContext.AddTotbl_areaitemmap(obj);
try
{
_entitiesContext.SaveChanges();
return true;
}
catch (Exception)
{

return false;
}
}

推荐答案

如果您尝试上面的代码,那么您将得到一个解决方案.对于您的方案,您需要创建一个存储过程以将值插入表中并调用从实体框架存储p.
if you try the above code then you will get a solution.For your scenario you need create a store procedure for inserting the value in both table and call that store p from entity framework.


我不知道您说的是什么
当我将数据插入到tbl_areaitemmap中时,还会在tbl_item中插入一个,如果该项目存在,则tbl_areaitemmap会引用这些ID.

我已经使用了映射表,它的意思是这样.

首先,您插入tbl_item并获取其itemid.然后插入tbl_area并获取其areaid.
那么您可以添加一个tbl_areaitemmap和两个ID.

则实体框架将自行在内部为您链接对象.

这是样本

int itemid = 0;
tbl_item tableitem = new tbl_item();
tbl_areaitemmap areaitemmaps = new tbl_areaitemmap();

using(sampleEntities _entitiesContext = new SampleEntities())
{
//在此处设置表格项值


//在此处设置tableareaitemmaps值
areaitemmaps.tbl_area_r.AreaId = 7;

//除非tbr_itemId为12,否则您将无法插入Areaitemmap,除非
//如果您尝试添加Areaitemmaps,则ID为12的数据库中存在一个tbl_item
//如果tbl_item.Id不存在,则会抛出外键中断异常
//离开以下行
//areaitemmaps.tbl_item.pkItemId = 12;
areaitemmaps.status ="T";


试试
{
//首先插入tableitem

_entitiesContext.AddTotbl_item(tableitem);
_entitiesContext.SaveChanges();

//然后获取新插入的tableitem的ID并将其分配给areaitemmap
areaitemmap.tbl_item.pkItemId = tableitem.ItemId

//然后插入tableareaitemmap
_entitiesContext.AddTotbl_areaitemmap(areaitemmap);

_entitiesContext.SaveChanges();

//这将使其强制执行您的外键

返回true;
}
catch(Exception)
{

返回false;
}
}
i dont know what you mean when you say
when I''m insert data into into tbl_areaitemmap there is also a insertion in tbl_item and if the item is exist then tbl_areaitemmap refrence to those id.

i have used mapping tables and its meant to be done like this.

first you insert the tbl_item and get its itemid. then insert tbl_area and get its areaid.
then you can add a tbl_areaitemmap with the two ids you just got.

then entity framework will internally link the objects for you by itself.

here is sample

int itemid = 0;
tbl_item tableitem = new tbl_item();
tbl_areaitemmap areaitemmaps = new tbl_areaitemmap();

using(sampleEntities _entitiesContext = new SampleEntities())
{
//setup tableitem values here


//setup tableareaitemmaps values here
areaitemmaps.tbl_area_r.AreaId =7;

//you wont be able to insert a areaitemmap with tbl_itemId of 12 unless
//a tbl_item exists in database with a Id of 12. if you try to add a areaitemmaps
//with tbl_item.Id of a non existing one it will throw a foreign key broken exception
// leave the following line
//areaitemmaps.tbl_item.pkItemId = 12;
areaitemmaps.status = "T";


try
{
//firstly insert the tableitem

_entitiesContext.AddTotbl_item(tableitem);
_entitiesContext.SaveChanges();

//then get the Id of newly inserted tableitem and assign it to the areaitemmap
areaitemmap.tbl_item.pkItemId = tableitem.ItemId

//then insert tableareaitemmap
_entitiesContext.AddTotbl_areaitemmap(areaitemmap);

_entitiesContext.SaveChanges();

//this will make it enforce your foreign key

return true;
}
catch (Exception)
{

return false;
}
}


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

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