asp.net-mvc 在没有主键的情况下使用 Find 在 dbSet 中查找记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12545565/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:16:21  来源:igfitidea点击:

Find a record in dbSet using Find without a primary key

asp.net-mvclinqentity-framework

提问by Mortalus

I have a users table:

我有一个用户表:

Users:
 +ID
 +Username
 +...

I want to use myDBContext.Users.Find(Username)to fin a users. in my current context I can not use his ID.

我想用myDBContext.Users.Find(Username)fin 用户。在我目前的情况下,我不能使用他的 ID。

do i have to use a full LINQ query ? e.g.

我必须使用完整的 LINQ 查询吗?例如

var user = from users in myDBContext.Users.Find(Username) 
           where users.Username == username
           select users

I have also tried to define the username as a primary key in my edmx but that resulted in the following error:

我还尝试将用户名定义为我的 edmx 中的主键,但这导致了以下错误:

Properties referred by the Principal Role User must be exactly identical to the key of the EntityType CamelotShiftManagementModel.User referred to by the Principal Role in the relationship constraint for Relationship CamelotShiftManagementModel.AssociationUserFK1. Make sure all the key properties are specified in the Principal Role. C:\Code\CamelotShiftManagement\CamelotShiftManagement\Models\CamelotDB.edmx 278 11 CamelotShiftManagement

主体角色用户引用的属性必须与关系 CamelotShiftManagementModel.AssociationUserFK1 的关系约束中主体角色引用的 EntityType CamelotShiftManagementModel.User 的键完全相同。确保在 Principal Role 中指定了所有关键属性。C:\Code\CamelotShiftManagement\CamelotShiftManagement\Models\CamelotDB.edmx 278 11 CamelotShiftManagement

回答by Jignesh Thakker

Try with,

试试看,

User myUser = myDBContext.Users.SingleOrDefault(user => user.Username == username);

Use SingleOrDefaultinsted of Single. If user doesn't exist then Singlewill throw an error. While SingleOrDefaultwill return nullif user not found otherwise Userobject will be return.

使用SingleOrDefaultinsted的的Single。如果用户不存在,Single则会抛出错误。尽管SingleOrDefault将返回null如果用户没有找到,否则User对象将是回报。

Selection Between SingleOrDefaultand FirstOrDefault

SingleOrDefault和之间选择FirstOrDefault

You can get the user object by using SingleOrDefaultand FirstOrDefaultbut while selecting which method to use consider below points.

您可以使用SingleOrDefault和获取用户对象,FirstOrDefault但在选择使用哪种方法时,请考虑以下几点。

  • Both return only one value from collection/database if exist otherwise default value.
  • But if you have more than one user with same name and you are expecting to get an exception while performing LINQ query then use SingleOrDefaultas it will thrown an exception if there are more than one element available.
  • And if you don't want exception and/or you don't want to check that your collection/database have duplication of data, just want to get first value from collection/database then use FirstOrDefaultfor better performance compare to SingleOrDefault
  • 如果存在,则两者都仅从集合/数据库返回一个值,否则为默认值。
  • 但是,如果您有多个同名用户,并且您希望在执行 LINQ 查询时出现异常,请使用,SingleOrDefault因为如果有多个元素可用,它将引发异常。
  • 如果您不想要异常和/或您不想检查您的集合/数据库是否有重复的数据,只想从集合/数据库中获取第一个值然后FirstOrDefault用于更好的性能比较SingleOrDefault

FirstOrDefault

FirstOrDefault

  • Generally FirstOrDefaultor Firstused when we required single value (first) from the collection or database.
  • In the case of Fist / FirstOrDefault, only one row is retrieved from the database so it performs slightly better than single / SingleOrDefault. such a small difference is hardly noticeable but when table contain large number of column and row, at this time performance is noticeable.
  • 通常FirstOrDefaultFirst在我们需要来自集合或数据库的单个值(第一个)时使用。
  • 在 Fist / FirstOrDefault 的情况下,仅从数据库中检索一行,因此它的性能略好于 single / SingleOrDefault。如此小的差异几乎不会引起注意,但是当表包含大量列和行时,此时性能是显而易见的。

Some other remarks

其他一些备注

  • If usernameis primary key then I think there will be no much/no difference between SingleOrDefaultand FirstOrDefaultperformance as primary key has index and search on index column will always be faster than normal column.
  • Singleor SingleOrDefaultwill generate a regular TSQL like "SELECT ...".
  • The Firstor FirstOrDefaultmethod will generate the TSQL statment like "SELECT TOP 1..."
  • 如果username是主键,那么我认为SingleOrDefaultFirstOrDefault性能之间没有太大/没有区别,因为主键有索引并且索引列上的搜索总是比普通列快。
  • Single或者SingleOrDefault会生成一个像“SELECT ...”这样的常规TSQL。
  • FirstFirstOrDefault方法将产生TSQL statment像“SELECT TOP 1 ...”

回答by Mortalus

I've found it:

我找到了:

User myUser = myDBContext.Users.Single(user => user.Username == i_Username);