使用不带主键的查找在dbSet中查找记录

时间:2021-12-05 22:51:15

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(用户名)来完成用户。在我目前的背景下,我不能使用他的身份证。

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的密钥完全相同。确保在主体角色中指定了所有关键属性。 C:\ Code \ CamelotShiftManagement \ CamelotShiftManagement \ Models \ CamelotDB.edmx 278 11 CamelotShiftManagement

2 个解决方案

#1


40  

Try with,

试试吧,

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

Use SingleOrDefault insted of Single. If user doesn't exist then Single will throw an error. While SingleOrDefault will return null if user not found otherwise User object will be return.

使用SingleOrDefault insted of Single。如果用户不存在,那么Single将抛出错误。如果未找到用户,SingleOrDefault将返回null,否则将返回User对象。

Selection Between SingleOrDefault and FirstOrDefault

SingleOrDefault和FirstOrDefault之间的选择

You can get the user object by using SingleOrDefault and FirstOrDefault but 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 SingleOrDefault as it will thrown an exception if there are more than one element available.
  • 但是,如果您有多个具有相同名称的用户,并且您希望在执行LINQ查询时遇到异常,则使用SingleOrDefault,因为如果有多个可用元素,它将抛出异常。
  • 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 FirstOrDefault for better performance compare to SingleOrDefault
  • 如果你不想要异常和/或你不想检查你的集合/数据库是否有数据重复,只想从集合/数据库中获取第一个值,那么使用FirstOrDefault比SingleOrDefault更好的性能

FirstOrDefault

FirstOrDefault

  • Generally FirstOrDefault or First used when we required single value (first) from the collection or database.
  • 通常在我们从集合或数据库中需要单个值(第一个)时使用FirstOrDefault或First。
  • 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.
  • 在Fist / FirstOrDefault的情况下,只从数据库中检索到一行,因此它的性能略好于single / SingleOrDefault。如此小的差异几乎不可察觉,但当表包含大量的列和行时,此时性能是显而易见的。

Some other remarks

其他一些言论

  • If username is primary key then I think there will be no much/no difference between SingleOrDefault and FirstOrDefault performance as primary key has index and search on index column will always be faster than normal column.
  • 如果用户名是主键,那么我认为SingleOrDefault和FirstOrDefault性能之间没有太大差异,因为主键具有索引,索引列上的搜索总是比普通列快。
  • Single or SingleOrDefault will generate a regular TSQL like "SELECT ...".
  • Single或SingleOrDefault将生成常规TSQL,如“SELECT ...”。
  • The First or FirstOrDefault method will generate the TSQL statment like "SELECT TOP 1..."
  • First或FirstOrDefault方法将生成TSQL语句,如“SELECT TOP 1 ...”

#2


4  

I've found it:

我找到了:

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

#1


40  

Try with,

试试吧,

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

Use SingleOrDefault insted of Single. If user doesn't exist then Single will throw an error. While SingleOrDefault will return null if user not found otherwise User object will be return.

使用SingleOrDefault insted of Single。如果用户不存在,那么Single将抛出错误。如果未找到用户,SingleOrDefault将返回null,否则将返回User对象。

Selection Between SingleOrDefault and FirstOrDefault

SingleOrDefault和FirstOrDefault之间的选择

You can get the user object by using SingleOrDefault and FirstOrDefault but 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 SingleOrDefault as it will thrown an exception if there are more than one element available.
  • 但是,如果您有多个具有相同名称的用户,并且您希望在执行LINQ查询时遇到异常,则使用SingleOrDefault,因为如果有多个可用元素,它将抛出异常。
  • 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 FirstOrDefault for better performance compare to SingleOrDefault
  • 如果你不想要异常和/或你不想检查你的集合/数据库是否有数据重复,只想从集合/数据库中获取第一个值,那么使用FirstOrDefault比SingleOrDefault更好的性能

FirstOrDefault

FirstOrDefault

  • Generally FirstOrDefault or First used when we required single value (first) from the collection or database.
  • 通常在我们从集合或数据库中需要单个值(第一个)时使用FirstOrDefault或First。
  • 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.
  • 在Fist / FirstOrDefault的情况下,只从数据库中检索到一行,因此它的性能略好于single / SingleOrDefault。如此小的差异几乎不可察觉,但当表包含大量的列和行时,此时性能是显而易见的。

Some other remarks

其他一些言论

  • If username is primary key then I think there will be no much/no difference between SingleOrDefault and FirstOrDefault performance as primary key has index and search on index column will always be faster than normal column.
  • 如果用户名是主键,那么我认为SingleOrDefault和FirstOrDefault性能之间没有太大差异,因为主键具有索引,索引列上的搜索总是比普通列快。
  • Single or SingleOrDefault will generate a regular TSQL like "SELECT ...".
  • Single或SingleOrDefault将生成常规TSQL,如“SELECT ...”。
  • The First or FirstOrDefault method will generate the TSQL statment like "SELECT TOP 1..."
  • First或FirstOrDefault方法将生成TSQL语句,如“SELECT TOP 1 ...”

#2


4  

I've found it:

我找到了:

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