title: New record using Entity Framework DataLayer Repository Add() Function
body: <p>I am beginner to Entity Framework and ASP.NET MVC and learning it from online tutorials, I made a class library datalayer (<code>Student.DataLayer</code>) first, in which I have a <code>Repository.cs</code>, where I have all the functions like </p>
  
  <pre><code>protected readonly ObjectContext _connection;
  public Repository(IConnectionHelper connection)
  {
      _connection = connection.Connection;
  }
  
  public virtual ObjectSet&lt;T&gt; ObjectSet
  {
      get
      {
          return
  
          _connection.CreateObjectSet&lt;T&gt;();
      }
  }
  
  public virtual T Get(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
  {
      return ObjectSet.FirstOrDefault(predicate);
  }
  
      public virtual T Add(T entity)
      {
          ObjectSet.AddObject(entity);
          Save();
          return entity;
      }
  
      public virtual void Save()
      {
          _connection.SaveChanges();
  
       }
  </code></pre>
  
  <p>in <code>IRepository.cs</code> I have </p>
  
  <pre><code>public interface IRepository&lt;T&gt; where T : class
  {
      T Get(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
      T Add(T entity);
      void Save();
  }
  </code></pre>
  
  <p>Now I have add the reference of <code>Student.Datalayer</code> in the main ASP.NET MVC3 project <code>Student</code>, so I can access all the functions of datalayer now.</p>
  
  <p>I have a table in database with the name <code>Student</code>, and it only have <code>student_id</code> and <code>student_first_name</code> and <code>student_last_name</code> columns</p>
  
  <p>So far I have managed to update records in model which are already added</p>
  
  <pre><code>    public bool update_name(int id, string newfirstname, string newlastname)
      {
          var stud = _stu.Get(p =&gt; p.student_id == id);
           stud.student_first_name = newfirstname;
           stud.student_last_name = newlastname;
          _stu.Save();
          return true;
      }
  </code></pre>
  
  <p>but how to add a new record in the table using <code>_stu.Add()</code>, I can't figure this out. I am trying like below.</p>
  
  <pre><code>    public bool add_Student(string firstname, string lastname)
      {
         _stu.Add( //what to do here
         return true;
      }
  </code></pre>
  
  <p>I really want to know this way, because using datalayer like this approach, it's very clean. So please help me</p>