0

I'm new to mvc.Before i used to code with entity framewok and it's easy.But i have to do some different things.I have looked some tutorials but i couldn't understand very well and they don't cover my needs. I connected my Visual studio and got data from oracle 12c successfully using by Oracle Data Provider for .net,not by using entity framework.Code is at above.

string constr = "Data Source=localhost/orcl;User Id=HOSPITAL;Password=HOSPITAL;";
OracleConnection conn = new OracleConnection();
OracleCommand cmd;
OracleDataAdapter da;
conn.ConnectionString = constr;
OracleCommandBuilder cb;
DataSet ds;    
conn.Open();

string sql = "Select * from BABY
IDENTITY.FIRSTNAME,IDENTITY.LASTNAME,IDENTITY.MOTHERNAME,IDENTITY.FATHERNAME,
IDENTITY.BIRTHDAY,IDENTITY.GENDER,BABY.WEIGHT,BABY.HEIGHT FROM BABY 
INNER JOIN IDENTITY ON BABY.FILE_NO=IDENTITY.FILE_NO";

cmd = new OracleCommand(sql, conn);
cmd.CommandType = CommandType.Text;

da = new OracleDataAdapter(cmd);
cb = new OracleCommandBuilder(da);
ds = new DataSet();

da.Fill(ds);

conn.Close();

I want to bind this data to my model and display at view.My model is as the following class.

public class Baby
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthday { get; set; }
    public string FatherName { get; set; }
    public string MotherName { get; set; }
    public string Gender { get; set; }
    public int Height { get; set; }
    public int Weight { get; set; }

 }

How can we bind dataset to my model or how can i bind my query result directly to my model?

Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124

1 Answers1

0

Entity framework can be used with oracle: http://download.oracle.com/oll/obe/EntityFrameworkOBE/EntityFrameworkOBE.htm

you could use an adapter that set's all the model properties from the datatable eg:

var dt = new DataTable();
dt = ds.Tables[0];
IList<Baby> Babies = new List<Baby>();
foreach(DataRow dr in dt.Rows)
{
var baby= new Baby();
baby.FirstName = dr["Baby firstname column name from db"].ToString();
baby.Weight= (int)dr["Baby weight column name from db"];

Babies.Add(baby);
}

Or use Automapper to do this for you if you don't want to hand code a lot of left right code. Using AutoMapper with DataTable with missing columns http://automapper.org/

Community
  • 1
  • 1
iceburg
  • 1,768
  • 3
  • 17
  • 25
  • I can't use entity framework because it needs primary key at the tables but they don't have and i am not allowed to change table design. –  Oct 02 '14 at 23:09
  • oh, ok. U can use the second approach then. – iceburg Oct 02 '14 at 23:14