VB Magic

2011/06/27

Trying to get Entity Framework Code First working : The Solution

Filed under: ASP.NET MVC, Azure, Learning — Tags: , , — vbmagic @ 11:39 am

Hi,

In the light of day the problem is solved. It seems that if the database you are connecting to already exists, then it will not create any new tables that you may try to access.

I basically dropped my local database and re-ran the webserver and it created the tables and the database with no issues.

On with the MVC3 course…

Jas

2011/06/26

Trying to get Entity Framework Code First working : The Problem

Filed under: ASP.NET MVC, Azure, Learning — Tags: , , , , — vbmagic @ 10:44 pm

I’ve added reference to EntityFramework (Downloaded by NuGet) to my MVC 3 project.

I then created the following two classes to represent my tables:

Imports System.ComponentModel.DataAnnotations

Public Class tImage
    <Key()>
    Public Property ID As Int64

    <MaxLength(50)>
    Public Property name As String

    <MaxLength(255)>
    Public Property URL As String

    Public Property type As tImageType

    <MaxLength(50)>
    Public Property tag As String
End Class
Imports System.ComponentModel.DataAnnotations

Public Class tImageType
    <Key()>
    Public Property ID As Integer

    <MaxLength(50)>
    Public Property name As String

    <MaxLength(255)>
    Public Property description As String

End Class

And then created the following class as my data context:

Imports System.Data.Entity

Public Class TyranntDB
    Inherits DbContext

    Public Property images As DbSet(Of tImage)
    Public Property imageTypes As DbSet(Of tImageType)

End Class

I then added the following connection string to my web.config file:

  <connectionStrings>
             .....
    <add name="TyranntDB"
         connectionString="Data Source=.\SQL08;Initial Catalog=jsundb;Integrated Security=True;Pooling=False"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

Everything from this point compiled ok. After this I created an Image Controller

Namespace TyranntServices
    Public Class ImageController
        Inherits System.Web.Mvc.Controller


        Dim _db As New TyranntDB
        '
        ' GET: /Image

        Function Index() As ActionResult

            Dim model = _db.images

            Return View(model)
        End Function
...

And the following View to list Images (Well it was mostly auto generated by using the Add View option with list template):

@ModelType IEnumerable(Of TyranntServices.tImage)

@Code
    ViewData("Title") = "Index"
End Code

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p> 
<table>
    <tr>
        <th></th>
        <th>
            name
        </th>
        <th>
            URL
        </th>
        <th>
            tag
        </th>
    </tr>

@For Each item In Model
    @<tr>
        <td>
            @Html.ActionLink("Edit", "Edit", New With {.id = item.ID}) |
            @Html.ActionLink("Details", "Details", New With {.id = item.ID}) |
            @Html.ActionLink("Delete", "Delete", New With {.id = item.ID})
        </td>
        <td>
            @item.name
        </td>
        <td>
            @item.URL
        </td>
        <td>
            @item.tag
        </td>
    </tr>
Next
</table>

This all seems to compile well. When I run though, the tables do not get generated and I get an error stating that the dbo.tImages does not exist.

It’s late now so will post this and hope I can soon update it with what is wrong.

Jas

« Newer Posts

Blog at WordPress.com.