实体框架查找表 [英] Entity Framework lookup table

查看:111
本文介绍了实体框架查找表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表

Brands:
BrandID int
BrandName varchar(30)

Products
ProdID int
ProdName varchar(30)

BrandToProd:
BrandID int => FK Brands.BrandID
ProdID int => FK Products.ProdID

从现有数据库生成EF模型后忽略BrandToProd表,并创建品牌和产品之间的许多一对多的关系。我想有三实体以下字段:

After generating model from existing database EF omits BrandToProd table and creates Many-To-Many relationships between Brands and Products. I would like to have third entity with following fields:

BrandName varchar(30)
ProductsName varchar(30)

这会给我可能使用脚手架这个实体。理想的情况是,当我将添加新的对品牌和产品的,EF应该先检查是否这种品牌或产品数据库中已存在(以避免重复),如果没有,添加到相应的表和添加映射BrandToProd表。如果品牌或产品已经存在,EF应该增加BrandToProd表时,你现有的BrandID /的ProdID值。是否有任何想法如何做到这一点?

This will give me possibility to use scaffolding for this entity. Ideally, when I'll add new pair of Brand and Product, EF should check first if such Brand or Product already exist in database (to avoid duplicates), if no, add to corresponding tables and that add mapping to BrandToProd table. If Brand or Product already exist, EF should you existing BrandID/ProdID value when adding to BrandToProd table. Is there any idea how to do that?

推荐答案

我已经结束了与刚刚加入虚拟ID字段来我结合表,因为我经常变化的数据库架构(becsuse网站的发展过程中,我需要时常从数据库更新时间模型),不想每次都删除/虚拟字段添加到数据库中。我用另一种选择 - 对两个表,并在EF映射到相应的操作(CRUD)存储过程SQL视图

I've ended up with just adding dummy ID field to my junction table, as I'm frequently changing DB schema (becsuse site development is in progress and I need from time to time update model from database) and don't want to each time remove/add dummy field to database. Another option I've used - SQL View on two tables and stored procedures mapped to corresponding actions (CRUD) in EF

这篇关于实体框架查找表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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