I have been looking forward to this for a while, and tonight i finally got around to installing Visual Studio 2010 and .Net 4.0 Beta 1 – including the new Entity Framework bits.
You may remember that i was not completely sold on Entity Framework 1.0, and neither was a big part of the developer community, so there are of course a lot of areas that I am really looking forward to explore in this coming version of the framework.
First and foremost I was very curious as to whether the Entity Framwork offers the promised POCO scenario, so as a first encounter I set of to figure out if this is the case or not.
I started out with creating a solution with only one project called “Model” containing a single class “Customer”:
6 namespace EntityFrameworkPoco.Model
7 {
8 public class Customer
9 {
10 public int Id { get; set; }
11 public string FirstName { get; set; }
12 public string LastName { get; set; }
13 public int Age { get; set; }
14 }
15 }
So now I have defined a simple Poco class that i want to use with the Entity Framework. I am not going to change this class in any way.
Next step is to create the model. I can create the metadata files myself or I can use the designer. I dont have a database with customers, and as I want to test another feature of the new Entity Framework I go with the designer:
So I create another project that I call EntityFramework, and to that i add a new ADO.Net Entity Data Model.
I select the .edmx file in the solution explorer, and in the Entity Framework Designer interface i create my Customer entity replicating my actual POCO entity, and as i dont have a database for my customers i right click and choose “Generate Database Script from Model”.
I choose my database connection and let it create a new database for me. The dialog then generates the SQL script needed to create the model in my database. Neat.
So now I have plugged in the EF to allow me to persist my POCO object to database, and materialize it back to me. One thing we need to remember is, that we don’t want the Entity Data Model we just created, to generate code for us – we want to handle that ourselves – well at least for now.
Therefore we remove the “Custom Tool” that generates the code. Just delete the content of the property. This turns of code generation for the model.
So far so good. Now we need to hook it all up. We are going to need a context implementation now that it is not generated for us. So lets create the the CustomerContext on our EntityFramework project and let it expose a collection of our POCO Customers – an ObjectSet of Customer that is:
10 public class CustomerContext : ObjectContext
11 {
12 private ObjectSet<Customer> _customers;
13
14 public CustomerContext():
15 base("name=CustomerModelContainer",
16 "CustomerModelContainer")
17 {
18 _customers
19 = CreateObjectSet<Customer>();
20 }
21
22 public ObjectSet<Customer> Customers
23 {
24 get
25 {
26 return _customers;
27 }
28 }
29
30 }
Too simple of course, but for now it will let us fetch customers from database. Behind the scenes i cheat and create a few in the database, so that we can test it.
36 static void Main(string[] args)
37 {
38 var context = new CustomerContext();
39 var customers = from c in context.Customers
40 select c;
41
42 foreach (var customer in customers)
43 {
44 Console.WriteLine(customer.FirstName);
45 }
46 Console.ReadKey();
47 }
Which results in the following output.
Now if we want to add new customers we could add a method to our context like so:
50 public void AddCustomer(Customer customer)
51 {
52 _customers.AddObject(customer);
53 }
Which enables us to add a new customer:
41 var newCustomer = new Customer();
42 newCustomer.FirstName = "Jim";
43 newCustomer.LastName = "Test";
44 newCustomer.Age = 42;
45 context.AddCustomer(newCustomer);
46 context.SaveChanges();
This is of course a very superficial example of POCO with the entity framework, but it is indeed very promising. I will be looking further into some more advanced examples in the near future.
Remember Me