0

I am practicing Entity Framework. I am practicing Asp.net MVC framework using Razor Engine and EF Code First. In practicing it, I found to have few problems. The problem is the Entity Framework doesnt creates a database in my SQL. I am using VisualStudio 2011 Beta Version with MSSQL 2008. I dont know what the problem is. Help required. Below is my Code :

    Person.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;

namespace MvcDemo.Data
{
    public class Person
    {
        [Key]
        public int PersonID { get; set; }

        [Required]
        [StringLength(40)]
        public string FirstName { get; set; }

        [Required]
        [StringLength(30)]
        public int LastName { get; set; }

        public int? Age { get; set; }

        [ForeignKey("PrefixID")]
        public Prefix Prefix { get; set; }


        [Required]
        public int PrefixID { get; set; }

    }
}

    Prefix.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;

namespace MvcDemo.Data
{
    public class Prefix
    {
        public int PrefixID { get; set; }

        [Required]
        [StringLength(20)]
        public string PrefixName { get; set; }
    }
}

MvcContext.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using MvcDemo.Data;


namespace MvcDemo.DataAccess
{
    public class MvcContext : DbContext
    {
        public MvcContext()
            : base("name = MvcDemoApp")
        {
        }
        public DbSet<Person> People { get; set; }

        public DbSet<MvcDemo.Data.Prefix> Prefix { get; set; }

    }
}

ConnectionString:
<connectionStrings>
    <add name="MvcDemoApp" 
         connectionString="Server=.;Initial Catalog=MvcDemoApp;Integrated Security=true" 
  providerName="System.Data.SqlClient" />

3 Answers 3

1

If this is all of your code it is not surprising that a database is not created. There is no code that executes commands against the database. As soon as you start iterating over e.g. MvcContext.People or do some insert you will notice that the database gets created.

Sign up to request clarification or add additional context in comments.

Comments

1

There are plenty step-by-step examples online. I would pick one of them and do a walk through; you can try this one for example: http://msdn.microsoft.com/en-us/data/gg685467.aspx. It should cover most of the basics to get you started.

If you prefer video over text, search Channel 9, or pluralsight

Comments

0

You should operate package manager console to do it.

Ope package manager console window and run "update-database" command, there still a bug in this command so you need to specify "startupprojectname" param to execute it and connection string in you DbContext file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.