查找使用查找dbSet创纪录的无主键 [英] Find a record in dbSet using Find without a primary key

查看:238
本文介绍了查找使用查找dbSet创纪录的无主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户表:

 用户:
 + ID
 +用户名
 + ...

我想用 myDBContext.Users.Find(用户名)来鳍用户。
在我目前的情况下,我不能用他的身份证。

我一定要使用完整的LINQ查询?例如。

  VAR用户=从myDBContext.Users.Find用户(用户名)
           其中,users.Username ==用户名
           选择用户

我也试图定义的用户名在我的EDMX一个主键,但是,导致以下错误:


  由校长角色用户​​称

属性必须完全
  完全相同的EntityType的关键
  CamelotShiftManagementModel.User提到了在主体作用
  对于关系的关系约束
  CamelotShiftManagementModel.AssociationUserFK1。确保所有关键
  属性中的主体指定
  角色。 C:\\ code \\ CamelotShiftManagement \\ CamelotShiftManagement \\型号\\ CamelotDB.edmx 278 11 CamelotShiftManagement



解决方案

与尝试,

 用户MYUSER = myDBContext.Users.SingleOrDefault(用户=> user.Username ==用户名);

使用的SingleOrDefault insted的的。如果用户不存在,那么将抛出一个错误。而的SingleOrDefault 将返回如果用户没有找到,否则用户对象将是回报。

选择之间的的SingleOrDefault FirstOrDefault

您可以通过获取用户对象的SingleOrDefault FirstOrDefault ,但在选择哪种方法来使用考虑以下要点


  • 双方是否存在,否则默认值返回从收集/数据库只有一个值。

  • 但是,如果你有相同名称的多个用户,并且期望获得一个例外,同时执行LINQ查询然后用的SingleOrDefault ,因为它会抛出一个异常,如果有有一个以上的元素可用。

  • 如果你不想异常和/或你不想检查您的收藏/数据库具有数据的重复,只是想从收藏第一个值/数据库,然后使用 FirstOrDefault 获得更好的性能比较的SingleOrDefault

FirstOrDefault


  • 一般 FirstOrDefault 首先当我们需要从收集或数据库单一值(第一个)使用。

  • 在拳/ FirstOrDefault的情况下,只有一行从数据库所以它的性能比单/的SingleOrDefault稍好检索。这样一个小的差异不太明显,但是当表中包含大量的行和列的,此时表现明显。

其他的一些言论


  • 如果用户名是主键,那么我认为不会有太大/无的SingleOrDefault 之间的区别 FirstOrDefault 性能,主键,索引和搜索的索引列总是会比正常速度快列

  • 的SingleOrDefault 将产生一个常规TSQL像SELECT ...。

  • 首先 FirstOrDefault 方法将产生TSQL说明书,如SELECT TOP 1 ...

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.

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

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:

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

解决方案

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.

Selection Between SingleOrDefault and FirstOrDefault

You can get the user object by using SingleOrDefault and FirstOrDefault but while selecting which method to use consider below points.

  • 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.
  • 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

  • Generally FirstOrDefault or First used 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.

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.
  • Single or SingleOrDefault will generate a regular TSQL like "SELECT ...".
  • The First or FirstOrDefault method will generate the TSQL statment like "SELECT TOP 1..."

这篇关于查找使用查找dbSet创纪录的无主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆