Entity framework DbContext未提供数据库中的任何数据
我有两套密码。第一个没有给我数据列表,但第二个有。请参阅以下代码: 第一个代码: 型号Entity framework DbContext未提供数据库中的任何数据,entity-framework,Entity Framework,我有两套密码。第一个没有给我数据列表,但第二个有。请参阅以下代码: 第一个代码: 型号 public class Student { [Key] public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public string Gender
public class Student
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
}
数据连接
public class DataConnection : DbContext
{
public DataConnection()
: base("DefaultConnection")
{
}
public DbSet<Student> Students { get; set; }
}
公共类数据连接:DbContext
{
公共数据连接()
:base(“默认连接”)
{
}
公共数据库集学生{get;set;}
}
接口
public interface IStudent
{
List<Student> StudentList();
void InsertStudent(Student student);
void UpdateStudent(Student student);
Student GetStudentById(int id);
void DeleteStudent(int id);
}
readonly DataConnection _context;
public StudentConcrete()
{
_context = new DataConnection();
}
public List<Student> StudentList()
{
var studentList = (from s in _context.Students select s).ToList();
return studentList;
}
readonly DataConnection _context;
public StudentConcrete()
{
_context = new DataConnection();
}
public List<Student> StudentList()
{
SqlConnection xxx = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
var cmd = new SqlCommand("GetAllStudents", xxx);
var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
return (from DataRow row in ds.Tables[0].Rows
select new Student()
{
Age = Convert.ToInt32(row["Age"]),
FirstName = row["FirstName"].ToString(),
Gender = row["Gender"].ToString(),
LastName = row["LastName"].ToString()
}).ToList();
}
else
{
return null;
}
}
公共接口是学生的
{
List StudentList();
无效插入学生(学生);
无效更新学生(学生);
学生GetStudentById(int id);
无效删除学生(内部id);
}
混凝土
public interface IStudent
{
List<Student> StudentList();
void InsertStudent(Student student);
void UpdateStudent(Student student);
Student GetStudentById(int id);
void DeleteStudent(int id);
}
readonly DataConnection _context;
public StudentConcrete()
{
_context = new DataConnection();
}
public List<Student> StudentList()
{
var studentList = (from s in _context.Students select s).ToList();
return studentList;
}
readonly DataConnection _context;
public StudentConcrete()
{
_context = new DataConnection();
}
public List<Student> StudentList()
{
SqlConnection xxx = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
var cmd = new SqlCommand("GetAllStudents", xxx);
var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
return (from DataRow row in ds.Tables[0].Rows
select new Student()
{
Age = Convert.ToInt32(row["Age"]),
FirstName = row["FirstName"].ToString(),
Gender = row["Gender"].ToString(),
LastName = row["LastName"].ToString()
}).ToList();
}
else
{
return null;
}
}
只读数据连接\u上下文;
公立学生混凝土()
{
_context=newdataconnection();
}
公开名单学生名单()
{
var studentList=(从_context.Students中选择s.ToList();
返回学生名单;
}
第二代码
混凝土
public interface IStudent
{
List<Student> StudentList();
void InsertStudent(Student student);
void UpdateStudent(Student student);
Student GetStudentById(int id);
void DeleteStudent(int id);
}
readonly DataConnection _context;
public StudentConcrete()
{
_context = new DataConnection();
}
public List<Student> StudentList()
{
var studentList = (from s in _context.Students select s).ToList();
return studentList;
}
readonly DataConnection _context;
public StudentConcrete()
{
_context = new DataConnection();
}
public List<Student> StudentList()
{
SqlConnection xxx = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
var cmd = new SqlCommand("GetAllStudents", xxx);
var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
return (from DataRow row in ds.Tables[0].Rows
select new Student()
{
Age = Convert.ToInt32(row["Age"]),
FirstName = row["FirstName"].ToString(),
Gender = row["Gender"].ToString(),
LastName = row["LastName"].ToString()
}).ToList();
}
else
{
return null;
}
}
只读数据连接\u上下文;
公立学生混凝土()
{
_context=newdataconnection();
}
公开名单学生名单()
{
SqlConnection xxx=新的SqlConnection(ConfigurationManager.ConnectionString[“DefaultConnection”].ConnectionString);
var cmd=新的SqlCommand(“GetAllStudents”,xxx);
var da=新的SqlDataAdapter(cmd);
var ds=新数据集();
da.填充(ds);
如果(ds.Tables[0].Rows.Count>0)
{
返回(来自ds.Tables[0]中的DataRow行)
选择新学生()
{
Age=Convert.ToInt32(第[“Age”行]),
FirstName=行[“FirstName”]。ToString(),
性别=行[“性别”]。ToString(),
LastName=行[“LastName”]。ToString()
}).ToList();
}
其他的
{
返回null;
}
}
我想使用第一个代码获取数据,但我不知道哪里出错了。我的SP只是为了得到学生。我怀疑您可能是在从另一个表中检索记录。是否尝试为学生实体添加
表
属性
using System.ComponentModel.DataAnnotations.Schema;
[Table("Students")]
public class Student
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
}
有什么错误吗?另外,“newsqlcommand(“GetAllStudents”,xxx);“什么是GetAllStudents?它是一个存储过程。如果我使用ADO.net方式(第二个代码),我会得到列表。但是,如果我将实体框架与LINQ(第一个代码)一起使用,我将不再获得列表。我正在取学生表中的记录。对于实体框架,我确实需要存储过程。我没有得到任何错误。只有0个列表。SP只是从学生中选择*而已。是否共享学生实体?请查看我的更新,其中包括学生模型。哦。我忘了。非常感谢你。