简体   繁体   English

如何覆盖默认 LINQ 到 SQL 关联名称

[英]How To Override Default LINQ to SQL Association Name

I am working on a pretty straight forward C# application that uses LINQ to SQL for database access.我正在开发一个非常简单的 C# 应用程序,它使用 LINQ 到 SQL 进行数据库访问。 The application is a non-web (ie thick client) application.该应用程序是一个非网络(即胖客户端)应用程序。

The problem that I have recently run into is with the default association name that LINQ to SQL is creating for fields that are foreign keys to another table.我最近遇到的问题是 LINQ 到 SQL 为另一个表的外键字段创建的默认关联名称。 More specifically, I have provided an example below:更具体地说,我在下面提供了一个示例:

Example of Problem The majority of my combo boxes are filled using values from a reference data.table (ie RefData) that stores a type, description, and a few other fields.问题示例 我的大部分组合框都使用参考 data.table(即 RefData)中的值填充,该参考存储类型、描述和一些其他字段。 When the form initially loads, it fills the combo boxes with values based on a query by type.最初加载表单时,它会根据类型查询用值填充组合框。 For example, I have a form that allows the user to add customers.例如,我有一个允许用户添加客户的表单。 On this form, there is a combo box for state. The stateComboBox is filled by running a query against the RefData.table where type = stateType.在此窗体上,有一个 state 的组合框。通过对 RefData.table 运行查询来填充 stateComboBox,其中类型 = stateType。 Then, when the user saves the customer with a selected state the id of the RefData column for the selected state is stored in the state column of the customer table.然后,当用户使用选定的 state 保存客户时,选定 state 的 RefData 列的 ID 存储在客户表的 state 列中。 All of this works as expected.所有这些都按预期工作。 However, if my customer table has more than one column that is a foreign key to the RefData.table it quickly becomes very confusing because the association name(s) created by LINQ are Customer.RefData, Customer.RefData1, Customer.RefData2, etc... It would be much easier if I could override the name of the association so that accessing the reference data would be more like Customer.State, Customer.Country, Customer.Type, etc...但是,如果我的客户表有多个列是 RefData.table 的外键,它很快就会变得非常混乱,因为 LINQ 创建的关联名称是 Customer.RefData、Customer.RefData1、Customer.RefData2 等...如果我可以覆盖关联的名称以便访问参考数据更像 Customer.State、Customer.Country、Customer.Type 等,那将会容易得多...

I have looked into changing this information in the DBML that is generated by VS but, my database schema is still very immature and constantly requires changes.我已经研究过在 VS 生成的 DBML 中更改此信息,但是,我的数据库架构仍然非常不成熟,并且不断需要更改。 Right now, I have been deleting the DBML every day or two to regenerate the LINQ to SQL files after making changes to the database.现在,我每隔一两天就会删除 DBML,以便在对数据库进行更改后重新生成 LINQ 到 SQL 文件。 Is there an easy way to create these associations with meaningful names that will not be lost while I frequently re-create the DBML?有没有一种简单的方法可以用有意义的名称创建这些关联,这些关联在我经常重新创建 DBML 时不会丢失?

Create a partial class definition for your Customer table and add more meaningful getter properties for the LINQ to SQL generated member names:为您的 Customer 表创建部分 class 定义,并为 LINQ 添加更有意义的 getter 属性到 SQL 生成的成员名称:

public partial class Customer
{
    public string Name { get; set; }
    [JsonIgnore]
    public RefData State => this.RefData;
    [JsonIgnore] 
    public RefData Country => this.RefData1;
}

I blogged about this here我在这里写了博客

I am not sure LINQ to SQL is the best method of accessing data, period, but I find it even more problematic in your case. 我不确定LINQ to SQL是访问数据的最佳方法,但是对于您来说,这甚至会带来更多问题。

Your real issue is you have the concept of your domain objects fairly static (you know what the program needs to use to get work done), but you are not sure how you are persisting the data, as your schema is in flux. 真正的问题是域对象的概念相当静态(您知道程序需要使用什么来完成工作),但是由于架构在不断变化,因此不确定如何持久化数据。 This is not a good scenario for automagic updates. 这不是自动更新的好方案。

If it were me, I would code the domain models so they do not change except when you desire change. 如果是我,我将对域模型进行编码,以使它们不会更改,除非您希望更改。 I would then determine how to link to the persistent schema (database in this case). 然后,我将确定如何链接到持久性架构(在这种情况下为数据库)。 If you like a bit more automagic, then I would consider Entity Framework, as you can use code first and map to the schema as it changes. 如果您需要更多的自动知识,那么我将考虑使用Entity Framework,因为您可以先使用代码,然后在更改时映射到架构。

If you find this still does not help, because your database schema changes are incompatible with the domain models, you need to get away from coding and go into a deeper planning mode. 如果您发现这仍然没有帮助,因为您的数据库架构更改与域模型不兼容,则需要摆脱编码并进入更深的计划模式。 Otherwise, you are going to continue to beat your head against the proverbial wall of change. 否则,您将继续在变革的众所周知的墙壁上打头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM