实体框架的插入,更新和删除操作 [英] Entity Framework Insert, Update and Delete Operation

查看:142
本文介绍了实体框架的插入,更新和删除操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个主表

1.客户主包含
客户编号(PK)
客户名称

2. Location Master包含
LocationId(PK)
LocationName

还有另一个表,如

CustomerLocation包含
客户编号(FK)
LocationId(FK)

当我插入ADO.NET数据模型时,它将仅为Customer和Location表创建实体
如果我想在CustomerLocation表中插入数据.如何插入?
我有一张表格,其中包含两个下拉菜单,一个用于客户",另一个用于位置"
我必须将其插入到CustomerLocation表中的数据

我遵循存储库结构
因此,借助包含
的DataRepository文件 T Add(T实体);
void Update(T实体);方法
如果我想为CustomerLocation创建数据存储库,该如何创建.因为没有
实体模型.

帮帮我.我是存储库结构的新手.

I have Two master Tables

1. Customer Master contains
CustomerId (PK)
CustomerName

2. Location Master Contains
LocationId (PK)
LocationName

And another table like

CustomerLocation contains
CustomerId (FK)
LocationId (FK)

When i insert ADO.NET Data model it will create entity for Customer and Location table only
if i want to insert data in CustomerLocation table. How can i insert?
I have one form which contain two dropdown one for Customer and one for Location
with which i have to insert data in CustomerLocation Table

I follow Repository Structure
So, With the help of DataRepository file which contain
T Add(T entity);
void Update(T entity); methods
if i want to create data repository for CustomerLocation how can i create. Because there is no
entity model for that.

Help me for that. I am new for Repository Structure.

推荐答案

请参考此链接以获取 ^ ],更新 [ ^ ]和删除 [链接 [ ^ ]
hi please refer this link for add[^] , Update[^] and Delete[^] code for ur understanding

if you want to perform this using store procedure use this deference link[^]


当Entity Framework找到一个链接表时,该链接表仅包含指向其他两个表的外键,它将从模型中忽略该表,而是在链接的表上创建n-m的关系.
因此,在您的模型中,您会期望:
-客户有n个CustomerLocations
-地点有n个CustomerLocations
-CustomerLocation有1位客户
-CustomerLocation具有1个位置

而是创建以下内容:
-客户有n个地点
-位置有n个客户

在这种情况下,要创建一个CustomerLocation,将一个位置添加到客户位置"或将一个客户"添加到位置客户"就足够了.

如果您确实需要CustomerLocation表,请考虑给它一个额外的列,例如ID字段.
在这种情况下,您的实体模型不会在Customer和Location上创建n-m关系,而是创建CustomerLocation表并提供"expected"输出.

我知道这一点是因为当我第一次开始使用Entity Framework时,我遇到了同样的问题" :)

这里''的链接也对此进行了解释: ^ ]
When Entity Framework locates a link table that only holds foreign keys to two other tables it will omit the table from the model and instead create an n-to-m relation on the tables that are linked.
So in your model you would expect:
- Customer has n CustomerLocations
- Location has n CustomerLocations
- CustomerLocation has 1 Customer
- CustomerLocation has 1 Location

Instead it creates the following:
- Customer has n Locations
- Location has n Customers

In this case to create a CustomerLocation it would be enough to add a Location to the Customers Locations or add a Customer to the Locations Customers.

If you really need the CustomerLocation table consider giving it an extra column, for example an ID field.
In this case your Entity Model won''t create the n-to-m relation on Customer and Location, but instead create the CustomerLocation table and give the ''expected'' output.

I know this because I''ve had the same ''problem'' when I first started using Entity Framework :)

Here''s a link that also explains this: Lack of a many-to-many linking table in EF 4.0 a bug or a feature?[^]


这篇关于实体框架的插入,更新和删除操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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