0
私は2組のコードを持っています。最初のものは私にデータのリストを与えませんが、2番目のものはデータのリストを与えません。DbContextがデータベースからデータを提供していません
まずコード: モデル
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; }
}
のdataconnection
public class DataConnection : DbContext
{
public DataConnection()
: base("DefaultConnection")
{
}
public DbSet<Student> Students { get; set; }
}
インタフェース
public interface IStudent
{
List<Student> StudentList();
void InsertStudent(Student student);
void UpdateStudent(Student student);
Student GetStudentById(int id);
void DeleteStudent(int id);
}
01以下のコードを参照してください。
コンクリート
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;
}
}
私は、最初のコードを使用してデータを取得したいと思いますが、私はそれが間違って取得する場所私は知りません。私のSPは学生を迎えることだけです。
間違いはありますか?また、 "新しいSqlCommand(" GetAllStudents "、xxx);" GetAllStudentsとは何ですか? – lucky
これはストアドプロシージャです。私がADO.netの方法(2番目のコード)を使用する場合、私はリストを取得します。しかし、LINQ(最初のコード)でエンティティフレームワークを使用すると、私はもうリストを取得しません。私は学生テーブルのレコードを取り出すだけです。エンティティフレームワークでは、私はストアドプロシージャが必要です。私は何の誤りもありません。ちょうど0リスト。 SPは生徒から*を選択するだけです。 –
学生エンティティを共有しますか? – lucky