Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Framework entity First
I just created a simple class library using Code First Entity Framework 4.1.
 
The db connection is in the console applcations app.config and the name of the connectionstring is "default".
So i initialized the DbContext like this -
public class EmployeeContext : DbContext
    {
       public EmployeeContext() : base("name=default") { }
        public List<employee> Employees { get; set; }
    }
I have referenced the Class Library in the console application. Then just try to add one employee.
class Program
    {
        static void Main(string[] args)
        {
            createEmp();
        }
 
        static void createEmp()
        {
            var obj = new EmployeeContext();
            Employee emp = new Employee();
            emp.ID = 1;
            emp.Name = "Test";
            obj.SaveChanges();
        }
    }
There is no error but the tables are not created in database.
 
When I look into the employeecontext, I see this exception -
ServerVersion '(((System.Data.Entity.DataContext)(obj)).Database.Connection).ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}
The connection is closed
.
 
This is my first hands on with code first entity framework.
 
Anyone has any idea of what is wrong?
Posted 13-Dec-11 4:38am
Edited 13-Dec-11 5:06am
RaisKazi33.1K
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There are two big problems with your code. I highly recommend that you watch or read some intro material on using Code First and EF. On msdn (msdn.com/data/ef) you can find some short intro videos on code first.
 
Problem #1: in the dbcontext, Employees must be a DbSet<Employee> for EF to be able to interact with the employee data.
 
Problem #2:Code First will initialize the database the first time EF needs to access it. But there will be no db access because you've created the employee but the obj (context) is unaware of it.
 
Before obj.SaveChanges, add the new employee so that the obj is aware of it with:
 
obj.Employees.Add(emp);
  Permalink  
Comments
Kochadayan at 13-Dec-11 12:34pm
   
Thanks for your response.After making the above changes and adding Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
the tables were created.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 386
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 215
3 Maciej Los 169
4 /\jmot 169
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 13 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100