0

I am trying to teach myself asp.net mvc and I am not sure how to do the right setup for the dbcontext class.

First I installed entity framework on my project. Then I created a model looking like this:

 public class Post
    {
        public int PostId { get; set; }

        public string Title { get; set; }

        public string Content { get; set; }

        public Post(int PostId, string Title, string Content)
        {
            this.PostId = PostId;
            this.Title = Title;
            this.Content = Content;
        }

Afterwards I created a class in the Models folder, called ClassAppContext.cs and after creating it I did a enable-migrations, here is how this class looks :

 public class ClassAppContext : DbContext
    {
        public DbSet<Post> Posts { get; set; }

    }

Can someone tell me if my approach was right, and if not, what did I do wrong?

Also, how can I create the database from these classes, I though it is autogenerated, but I don't see a DB in my project.

2

1 Answer 1

1

So a couple of things. You are on the right track.

You will need to override the OnModelCreating() method in your context class and also define a Initialization strategy along with the Entity Configuration Map (this is ideal to create it in a separate class, but you could have it in the method OnModelCreating as well). So your context class can look something like:

public class ClassAppContext : DbContext
{
    public DbSet<Post> Posts { get; set; }

    public ClassAppContext() : base("name=ClassAppDbContext")
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ClassAppContext>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove(new PluralizingTableNameConvention());
        modelBuilder.Configurations.AddFromAssembly(typeof(PostMap).Assembly);
    }
}

public class PostMap : EntityTypeConfiguration<Post>
{
    public PostMap()
    {
        ToTable("Post");
        HasKey(x => x.PostId)
            .Property(x => x.PostId)
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        Property(x => x.Title)
            .IsRequired()
            .HasColumnType("nvarchar")
            .HasMaxLength(250);

        Property(x => x.Content)
            .HasColumnType("nvarchar")
            .HasMaxLength(500);
    }
}

Your ClassAppDbContext is your connection string name defined in your web.config (also in app.config if you have context in a separate assembly).

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

9 Comments

Thanks for this answer. Will this code create the DB, or I'll have to do something extra?:)
You will have to enable entity framework migrations. So open the Package Manager console and type enable-migrations -enableautomaticmigrations. After that type update-database and that will create the database for your given that your connection string is correct and the project builds.
I would also suggest reading up on Entity Framework migrations on msdn.microsoft.com/en-us/library/jj554735(v=vs.113).aspx
To use "code first" in oder to create new db you do not need so much code - just add to the answer public ClassAppContext() : base("name=ClassAppDbContext") and set web.config/app.config e.g.: <connectionStrings> <add name="DBConnection" connectionString="data source=(localdb)\v11.0;Initial Catalog=poststore.mdf;Integrated Security=True;" providerName="System.Data.SqlClient"/> </connectionStrings>
NO, your context class name and your connection string name are two entirely different things. You pass the name of the connection string into the base class constructor from your context class by setting the name parameter.
|

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.