asp.net-mvc ASP.MVC db Find(),但具有非主键参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18545778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.MVC db Find(), but with non-primary key parameter
提问by user
How does one get a list of results by using a key that is not the primary key? To be more specific, I have a composite primary key where I would like to retrieve all the matches with one column's parameter.
如何通过使用非主键的键获得结果列表?更具体地说,我有一个复合主键,我想用一列的参数检索所有匹配项。
I would think, in an ActionResult in the Controller, it's something like
我认为,在控制器中的 ActionResult 中,它类似于
tableModel tbmodel = db.tableModels.Find(i => i.partialPK == parameter)
but that's not the case, since Find() only works with the entire PK.
但事实并非如此,因为 Find() 仅适用于整个 PK。
I declared my PKs in the entity model class as:
我在实体模型类中将我的 PK 声明为:
[Key]
[Column(Order = 0)]
public int PK1 { get; set; }
[Key]
[Column(Order = 1)]
public string PK2 { get; set; }
回答by Paul McCowat
According to DbSet.Findyou can pass in the primary keys separated by commas
根据DbSet.Find您可以传入以逗号分隔的主键
db.tableModels.Find(PK1, PK2)
The Find method takes an array of objects as an argument. When working with composite primary keys, pass the key values separated by commas and in the same order that they are defined in the model.
Find 方法将对象数组作为参数。使用复合主键时,传递以逗号分隔的键值,并按照它们在模型中定义的顺序进行传递。
However, if you want to just use one value, you will probably have to use
但是,如果您只想使用一个值,则可能必须使用
db.tableModels.Where(i => i.partialPK == parameter)
or an equivalent Linq operator
或等效的 Linq 运算符

