首页 新闻 会员 周边

Linq访问数据库报错,数据库通过Entity Framework建立

0
悬赏园豆:10 [已解决问题] 解决于 2015-03-16 17:16

代码如下:

 1        public ActionResult StuRegister(string stuId,string stuUserName)
 2         {
 3             using (var db = new DestineFoodServerDbContext())
 4             {
 5                 var student = db.Students.Find(stuId);
 6                 student.UserName = stuUserName ?? stuId;
 7                 var result = db.SaveChanges();
 8                 return Content(result == 1 ? ObjToJsonStr.ToJsonStr<Student>(student) : "数据库数据异常,请联系系统开发人员!");
 9             }
10         }

模型类Studen代码如下:

 1    public class Student
 2     {
 3         public int Id { get; set; }
 4         
 5         /// <summary>
 6         /// 学号
 7         /// </summary>
 8         public string StuId { get; set; }
 9 
10         /// <summary>
11         /// 学生姓名
12         /// </summary>
13         public string Name { get; set; }
14 
15         /// <summary>
16         ///  密码
17         /// </summary>
18         public string Password { get; set; }
19 
20         /// <summary>
21         /// 用户名
22         /// </summary>
23         public string UserName { get; set; }
24 
25         /// <summary>
26         /// 卡上余额
27         /// </summary>
28         public float CardBalance { get; set; }
29 
30         /// <summary>
31         /// 用户头像路径
32         /// </summary>
33         public string UserHeadImageUrl { get; set; }

 

调试时报错,错误如下所示:

The argument types 'Edm.Int32' and 'Edm.String' are incompatible for this operation. Near WHERE predicate, line 1, column 79.

显示错误在第5行。

模型类Student

Jack Leonardo的主页 Jack Leonardo | 初学一级 | 园豆:107
提问于:2015-03-16 16:07
< >
分享
最佳答案
0

你的主键是这个吧 public int Id { get; set; }

要用 public string StuId { get; set; } 查找的话,得 Find( o => o.StuId == struId)

收获园豆:10
Launcher | 高人七级 |园豆:45045 | 2015-03-16 16:43

@Launcher

你的意思是Find(stuId)是表示通过主键查询吗?

Jack Leonardo | 园豆:107 (初学一级) | 2015-03-16 17:07

@Jack Leonardo: 不是我的意思,请看这里:

https://msdn.microsoft.com/en-us/library/gg696418(v=vs.113).aspx

Launcher | 园豆:45045 (高人七级) | 2015-03-16 17:12

@Launcher: 

感谢,豁然了。

Jack Leonardo | 园豆:107 (初学一级) | 2015-03-16 17:16
其他回答(1)
0

楼上正解

茂茂 | 园豆:2892 (老鸟四级) | 2015-03-16 16:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册