查找使用查找dbSet创纪录的无主键 [英] Find a record in dbSet using Find without a primary key
问题描述
我有一个用户表:
用户:
+ 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 ofSingle
. If user doesn't exist thenSingle
will throw an error. WhileSingleOrDefault
will returnnull
if user not found otherwiseUser
object will be return.Selection Between
SingleOrDefault
andFirstOrDefault
You can get the user object by using
SingleOrDefault
andFirstOrDefault
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 toSingleOrDefault
FirstOrDefault
- Generally
FirstOrDefault
orFirst
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 betweenSingleOrDefault
andFirstOrDefault
performance as primary key has index and search on index column will always be faster than normal column.Single
orSingleOrDefault
will generate a regular TSQL like "SELECT ...".- The
First
orFirstOrDefault
method will generate the TSQL statment like "SELECT TOP 1..."这篇关于查找使用查找dbSet创纪录的无主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!