Wednesday, December 28, 2011

Entity Framework Code First C# and VB

This week I was trying to understand more about Entity Framework Code First. Nothing better that follow an example. Julie Lerman's video on Pluralsight have a great example in C# of code first. So, I decided to do the same in VB. However, before start I am going to show you how the project is organized:

Using NuGet we need to add the new EntityFramework library (which at this moment its version is 4.2) for these three projects.
In the Datalayer that is a class library we have a class named Context which has the following code:

   public class Context: DbContext     
        public DbSet<Header> Headers { get; set; }
        public DbSet<Detail> Details { get; set; }

In the DomainClass project we have the classes:
 public class Header
        public int Id { get; set; }
        public string Field { get; set; }

And Detail as:

 public class Detail
        public int Id { get; set; }
        public string FieldInDetail { get; set; }

In the console app we have the following code in the main:
           var header = new Header  { Field  = "Whatever you want to say" };
            var db = new Context();

So, I decided to run it and see how far we can get:

Excelent. When the project run for the first time is going to create a database in the SQLExpress instance. Something like this:

Easy. Of course we are not dealing with changes in the database when the model changes it. But, what happened when we translate this to

Ok, I need to find the answer of that. I will do that today or tomorrow and I will update this post. Of course somebody might have an answer before I do this.

Update: I just find the problem. My translation from C# to VB was not quite right:

I had:

Public Class Header
    Public Id As Integer
    Public Field As String
End Class
Public Class Detail
    Public Id As Integer
    Public FieldInDetail As String
End Class

Instead of:
Public Class Header
    Public Property Id() As Integer
    Public Property Field() As String
End Class
Public Class Detail
    Public Property Id() As Integer
    Public Property FieldInDetail() As String
End Class

I was confused because as old VB developer I had the very bad habit (or good) that if I did not want a property with setter and getter I use a public field and that was perfectly fine. Now if you want to declare a property in one line you should use the Property clause.

Perfect. Problem solved. It is 10:30 p.m. I am ready for watching T.V.


Thursday, December 1, 2011

Deserialize XML file from Reporting Services

I was trying to work with Reporting Services the other day. I have to make a comparison from a legacy application with my own process. Well, I cannot tell many details about it, but I wanted to upload an xml file from RSS to a class. I thought that it would be a good idea after render the file gets its data into a class using serialization. So I ended up doing the following (for this example I am using a silly report):

I create a report with one table from AdventureWorks database, in this case Address.

I saved the report as XML:

 After that I created a file using XSD tool. To do that, just go to the command tool of Visual Studio,

here is the command line
XSD address.xml
That command line is going to create another file named Address.xsd.  Then you can use the following command to create a VB or C# file.

XSD address.xsd /classes /language:vb

Attached the file produce by the command above into your project. I created a simple Console Application and I added to the main the following code:
Dim myReport As New Report
Dim objStreamReader As New StreamReader("C:\Development\temp\Address.xml")       
Dim x As New XmlSerializer(myReport.GetType)
myReport = x.Deserialize(objStreamReader)
I ran it and I got the following error

"Unable to generate a temporary class error CS0030. Cannot convert type Detail_CollectionDetail()() to Detail_CollectionDetail()" This is an image of the error:
I fix it removing an extra dimension in the Detail_CollectionDetail array. Using Quick Replace, I replaced ()()() for ()().
Running the app again, now I do not get error and I got the class populated.

Ok, this the way that I tried because I was curious following that path. It was not because is the best way to do this (maybe it is). You can play with LINQ to do the same, but that is another story.


Tuesday, November 29, 2011

The beginning

It is 11:30 P.M. I am going to start this blog with my own experiences in .NET. I want to share my thoughts, so people can send me feedback.
Why I started this blog. Because I have a really bad memory, and keeping track of how I do it will help me remember it better. However, this first post is a test post. I apologize if you were trying to find something interesting and you have not found it..yet.

My name is Richard Valdivieso M. I am a developer and I like to find things out. Let's begin Try-Fail-Triumph