Thursday, 15 November 2012

What is EF-Code First and CRUD?



 Yes, now from Entity Framework 4.1 onword we can work with Code First model also
Previously we generating the Entities from database then using Conceputal model of Entitieset Developer can able work with database.


App.config:

<connectionStrings>
    <add name="Conn" connectionString="uid=sa;pwd=zolt123$;database=EFRelations1;Trusted_Connection=true" providerName="System.Data.SqlClient" />
   
  </connectionStrings>

Code First Using Entity Framework 4.1 We can create database at completation of all our models.

EX :



 then CRUD:

Create a database EFRelations1 then Create Table then insert ing  the data.



 know CodeFirst

[key]----using System.ComponentModel.DataAnnotations;  

then 

Update : using System.Data;---------->EntityState

            Conn con = new Conn();
         
            Student sobjs  =con.Students.FirstOrDefault(i=>i.Sname=="Great1");
            sobjs.Sname = "PKB";
            con.Entry(sobjs).State = EntityState.Modified;
            con.SaveChanges();
 Delete:


   Student sobj = con.Students.Find(2);
            con.Students.Remove(sobj);
            con.SaveChanges();




No comments:

Post a Comment