在UI中使用列表框维护链接表 [英] Maintain link tables with listbox in the UI

查看:87
本文介绍了在UI中使用列表框维护链接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就最佳实践而言,这一直是我一直在努力的事情.

(为此目的简化了数据)

所以我有一个列表框A,其中包含颜色列表,包括红色,蓝色,黄色等.

用户可以选择一个或多个(红色和黄色),然后保存到数据库,该数据库保存UserID,CourourID x 2条记录.因此,红色(颜色ID 1),用户ID 1,黄色(颜色ID 2),用户ID1.

听起来还好吗?

因此,用户稍后会返回到表单.在页面的加载中,我再次将列表框与颜色绑定在一起,并通过阅读与用户1关联的链接表,可以看到需要选择红色和黄色,因此可以做到这一点.再次,非常简单.

但是,如果用户决定取消选择黄色和选择蓝色怎么办.我必须找到列表框的选定项,所以这次我将有蓝色(颜色ID 3),用户ID 1和黄色(颜色ID 2),用户ID1.

从技术上讲,我不需要对其中一个未更改的记录做任何事情,但是我是否将链接记录删除为红色,并插入一个将用户链接为蓝色的记录(似乎浪费了ID以保持删除和插入的作用)记录)?还是将链接记录中的颜色ID更改为3.如果他们只是决定仅保持黄色怎么办?这里有很多选择.我只是在与数据库保持持久地在列表框中选择的值的最佳方法/逻辑作斗争,而不必不断删除和插入行.也许有人有很酷的应对方式,或者我没有想到过的某种方式.

单个下拉列表不是问题,因为您在这里只处理一个记录.

顺便说一下,我正在使用Linq to SQL和C#3.5.

谢谢Louise.

This is something that I''ve always struggled with in terms of best practise.

(data simplified for the purposes of this)

So I have listbox A which contains a list of colours, red, blue, yellow etc.

User can choose one or more of these, so Red and Yellow and save to the database, which saves UserID, ColourID x 2 records. So Red (colour ID 1), UserID 1, Yellow (colour ID 2), UserID 1.

Sound Ok so far?

So the user goes back into the form later on. On the load of the page I bind the listbox again with the colours, and by reading that link table joined to User 1, I can see that Red and Yellow need to be selected, so I can do that. Again, pretty straight forward.

But what if the user decides to DESELECT Yellow and SELECT Blue. I would have to find the selected items of the listbox, So this time I''d have Blue (colour ID 3), UserID 1 and Yellow again (colour id 2), User ID 1.

Technically I dont need to do anything with one of the records, that hasnt changed, but do I delete the link record to the Red colour, and insert one linking the user to the Blue colour (seems a waste of IDs to keep deleting and inserting records)? Or do I change the colour ID in the link record to 3.. What if they just decide to Keep yellow only? There are many options here. I''m just struggling with the best way/logic to persist the values chosen in the listbox, with the database without constantly deleting and inserting rows. Maybe someone has a cool way of dealing with it or some kind of method I havent thought of.

Single drop downs are not an issue since you are only dealing with one record here.

I''m using Linq to SQL by the way, and C# 3.5.

Thanks, Louise.

推荐答案

最佳实践要求您删除任何链接表(很多到很多表)记录以取消选择颜色,并插入新记录. ID是否真的会成为您数据库中的问题?有这么多更改吗?如果是这样,请考虑使用Guid作为UserColour链接表的唯一键.另外,您可以使用用户ID"和颜色ID"组合作为唯一键,但是我从不喜欢它.

在许多情况下,我会先删除所有内容,然后重新插入新链接.这可能很有用,但并非永远不可取.

重用ID的问题在于,即使在链接表上,它也完全破坏了拥有唯一ID的意义.您可能会在将来包括与该链接有关的其他详细信息,例如CreatedBy,Created date或其他内容.

如果设置了要使用的颜色数,则可以使用单个按位值.我可以在这里详细解释,但是我想并不是您想要的或能够实现此数据结构的情况.如果要查找它,则那里有很多资源,.Net和SQL都可以使用按位操作数

希望对您有所帮助:D

按位I.E .:

红色= 1,黄色= 2,蓝色= 4
7& 2 = 2; //7有黄色
5& 2 = 0. //5不会
Best practice dictates that you delete any link table (many to many table) records for colours that are deselected and insert for new ones. Are IDs really going to be an issue in your db? Are that many changes made? If so, consider using a Guid for the unique key of the UserColour link table. Alternatively you could use the User Id and Colour Id combo as the unique key, but I have never liked that.

In many cases I delete everything first and re-insert the new links. This can be useful but isn''t ever desirable.

The issue with re-using the ID is that it completely undermines the point of having a unique ID, even on a link table. You may with to include other details pertaining to that link in the future such as CreatedBy, Created date or whatever.

If the number of colours you are using are set then you could use a single bitwise value. I can explain that here in length but I imagine that it is not the case that you want to, or are able to implement this data structure. If you want to look it up then there are plenty of resources out there and both .Net and SQL can use bitwise operands

Hope that helps :D

bitwise I.E.:

Red=1,Yellow=2,Blue=4
7&2=2; //7 has yellow
5&2=0. //5 does not


您是正确的,就删除和重新插入链接记录而言,就用户流量而言,这确实不是问题.他们可能也不会经常更改.那似乎有点笨拙,但这可能是此应用程序最简单的解决方案.谢谢您的回应.
you are right its not really going to be a problem in terms of user traffic, to delete and reinsert link records. They probably wont change then THAT often either..Just seems a bit clunky, but probably the easiest solution for this app. thank you for your response though.


这篇关于在UI中使用列表框维护链接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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