Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8052203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:29:19+00:00 2026-06-05T07:29:19+00:00

I am using EF4 code first within a simple MVC3 application. The correct connection

  • 0

I am using EF4 code first within a simple MVC3 application.
The correct connection string is being picked up by the context object, and the initializer constructor gets called, as does the OnModelCreating method, but the seed method is never called and the database is not created. Any ideas why?

protected void Application_Start()
{
    Database.SetInitializer<SchoolContext>(new SchoolInitializer());
    SchoolContext context = new SchoolContext();
    context.Database.CreateIfNotExists();
    ...
}

public class SchoolInitializer : DropCreateDatabaseAlways<SchoolContext>
{
    public SchoolInitializer()
    {
    }
    protected override void Seed(SchoolContext context)
    {
        var students = new List<Student>
        {
            new Student {StudentId = Guid.NewGuid(), Name = "Carson" },
            new Student {StudentId = Guid.NewGuid(), Name = "Meredith" }
        };
        students.ForEach(s => context.Students.Add(s));
        context.SaveChanges();
    }
}

public class Student
{
    [Key]
    public Guid StudentId;
    public string Name;
}

public class SchoolContext : DbContext
{
    public DbSet<Student> Students { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

Config:

<connectionStrings>
    <add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

The exception thrown I think is just complaining that the database has not been created:

[ModelValidationException: One or more validation errors were detected during model generation:

.Data.Edm.EdmEntityType: : EntityType ‘Student’ has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Students� is based on type �Student� that has no keys defined.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:29:20+00:00Added an answer on June 5, 2026 at 7:29 am

    Your model is invalid – Student entity has no key defined, and database creation is not triggered until model is valid.

    You should add accessors to your StudentId. This will make model valid and trigger database creation

    public Guid StudentId { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to teach my self MVC3 and EF4 using code first and
I just created a new project in MVC3 using EF4 code first deployed on
I am using EF4.3.1 and MVC4 code first. My database is being created except
I'm using EF4.1 Code First. I can't get a very simple insert to save
I have a database which is created using EF4.1 code first. The data context
I have a simple data model and I am using EF4.1 code first (but
Using VS2010, .NET4.0, MVC3, EF4.1 Code-First I have this POCO entities: public class XBLContent
I developed a website using EF 4.1 code first,Mvc3 ,Sql Sever 2008 r2, and
I would like to know the pros and cons of using EF4 Code-First approach.
When I first built the website, I was using EF4 Code First (without migrations).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.