使用LINQ to SQL处理“类型”表的最佳方法是什么?

时间:2020-11-23 19:36:10

I have some tables that represent various types. They usually just consist of an ID (int), and a name. Ideally I would have this in an enum. Is there a way to map a table like this onto an enum?

我有一些代表各种类型的表。它们通常只包含ID(int)和名称。理想情况下,我会在枚举中使用它。有没有办法将这样的表映射到枚举?

EDIT: How would I handle it if there were extra fields other than an ID and Name?

编辑:如果除了ID和名称之外还有其他字段,我将如何处理?

1 个解决方案

#1


1  

If it's just an id and a name, I generally do this:

如果它只是一个id和一个名字,我通常这样做:

public enum FootScent : int
{
Unknown = 0,
Mild = 1,
Sweaty =2,
SteppedInSomething = 3
}

and then on the LINQ entity property:

然后在LINQ实体属性上:

[Column("foot_scent_id", DbType = "Int NOT NULL")]
public FootScent Scent { get; set; }

For lookup tables with columns other than "id" and "name" that are needed, I usually just make a normal LINQ entity for them, though depending on your implementation it would probably be worth caching these to minimize trips to the DB.

对于需要除“id”和“name”之外的列的查找表,我通常只为它们创建一个普通的LINQ实体,但是根据您的实现,可能值得缓存这些以最小化到DB的访问。

#1


1  

If it's just an id and a name, I generally do this:

如果它只是一个id和一个名字,我通常这样做:

public enum FootScent : int
{
Unknown = 0,
Mild = 1,
Sweaty =2,
SteppedInSomething = 3
}

and then on the LINQ entity property:

然后在LINQ实体属性上:

[Column("foot_scent_id", DbType = "Int NOT NULL")]
public FootScent Scent { get; set; }

For lookup tables with columns other than "id" and "name" that are needed, I usually just make a normal LINQ entity for them, though depending on your implementation it would probably be worth caching these to minimize trips to the DB.

对于需要除“id”和“name”之外的列的查找表,我通常只为它们创建一个普通的LINQ实体,但是根据您的实现,可能值得缓存这些以最小化到DB的访问。