2

My models for my blog :

namespace AlexPeta_2.Models
{
    public class Article
    {
        public int ArticleId { get; set; }
        public string Title { get; set; }
        public string Text { get; set; }
        public char Published { get; set; }
        public DateTime CreatedDate { get; set; }
        public char AllowComment { get; set; }

        public virtual ICollection<Tag> Tags { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }
}

namespace AlexPeta_2.Models
{
    public class Comment
    {
        public int CommentId { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string HomePage { get; set; }
        public string Comm { get; set; }
        public DateTime CreatedDate { get; set; }
    }
}

namespace AlexPeta_2.Models
{
    public class Tag
    {
        public int TagId { get; set; }
        public string TagName { get; set; }
        public virtual ICollection<Article> Articles { get; set; }
    }
}

here are my dbsets:

public class BlogContext : DbContext
{
    public DbSet<Article> Articles { get; set; }
    public DbSet<Tag> Tags { get; set; }
    public DbSet<Comment> Comments { get; set; }
}

here is my initializer :

public class BlogInitializer : DropCreateDatabaseIfModelChanges<BlogContext>
    {
        protected override void Seed(BlogContext context)
        {

            List<Tag> tags = new List<Tag>
            {
                new Tag { TagId = 1, TagName = "Javascript" },
                new Tag { TagId = 2, TagName = "Bodybuilding" },
                new Tag { TagId = 3, TagName = "Uncategorised" },
                new Tag { TagId = 4, TagName = "CSS" },
                new Tag { TagId = 5, TagName = "HTML/XHTML" },
                new Tag { TagId = 6, TagName = "APEX" },
                new Tag { TagId = 7, TagName = "PL/SQL" },
                new Tag { TagId = 8, TagName = "Personal" },
                new Tag { TagId = 9, TagName = "ASP" },
                new Tag { TagId = 10, TagName = "MVC" },
                new Tag { TagId = 11, TagName = "C#" },
                new Tag { TagId = 12, TagName = "Snowboarding" }
            };
            tags.ForEach(t => context.Tags.Add(t));
            context.SaveChanges();
        }
    }

The funny thing is : i ran the application with no Connection String in webconfig, it runs fine , i see all my tags in the controller but there is not Database file in APP_DATA ?? How come is that? or what happened?

2 Answers 2

5

If you do not provide a connection string, EF Code First creates the database with user credentials (windows) on the local SQLExpress instance. You can validate the same by using SQL Server Management Studio Express and logging on to the local instance.

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

2 Comments

Got it. How can i force EF to make a new file under APP_DATA?
This tutorial shows how to do it with SQL Compact; for Express you do it the same way but use a SQL Express connection string that specifies |DataDirectory| like this one does: asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/…
2

Alex,

The connectionstring is actually derived automagically (i.e. by convention) from the dbcontext (in your case BlogContext), so you'll find that inside either sqlexpress or sqlcompact that you'll have a new db with the name that your dbcontext has.

good luck.

3 Comments

Thanks Jim, i got the issue. I need to install SQL Management Studio to view what is on the sql express instance as i've been using the Server Explorer pane in VS to view database connections and my BlogContext isn't to be found there nor in APP_DATA.
You were right Jimm and Prafulla was too. I was able to see the database create on the Express instance. Cheers
alex - i can feel your 'joy'. glad it all came together!!

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.