2012-08-31 76 views
1

我刚刚开始使用LINQ to SQL的一个项目,并且我遇到了多对多的 问题。我找到了很多可能的解决方案,并且我选择了this oneLINQ to SQL与属性的多对多关系 - C#

不管怎样,受影响的数据库表如下所示:

Customer (idcustomer,other stuff..) 
Customer_Role(idcustomer, idrole, other_attribute_I_want_to_keep) 
Role(idrole, other stuff) 

现在开始,我怎么能处理好“other_attribute_I_want_to_keep 这将是巨大的,有这样的:?

客户C。 Role.other_attribute_I_want_to_keep,但我看不出一个可行的解决方案。

感谢

回答

2

不能使用许多一对多 为了这。相反,您需要使CustomerRole成为客户和角色的第一类对象。客户和角色应分别声明CustomerRole的HasOne属性。然后,你可以这样做:

c.CustomerRole.Role 
c.CustomerRole.OtherAttribute 

您可以在Customer快捷Role属性定义为:

[NotMapped] 
public Role Role { 
    get { return CustomerRole == null ? (Role)null : CustomerRole.Role; } 
    set { if (CustomerRole == null) 
      CustomerRole = new CustomerRole(){ Role = value }; 
      else 
      CustomerRole.Role = value; 
    } 
} 

,但要知道,你可以构建一个复杂的查询时使用这个快捷方式遇到了麻烦。

+0

这将是一个解决方案,如果没有其他的可能,否则,是否有可能使“CustomerRole”字典而不是IEnumerable ? – ArtoAle

+0

我不确定你打算如何打算这样的字典,你能否详细说明一下? – PinnyM

+0

对不起,但CustomerRole属性怎么可能会好?我的意思是,这是一个集合,所以它会是CustomerRoleS,我不明白你定义的快捷方式属性 – ArtoAle