无法添加方法OnModelCreating

时间:2012-04-03 08:35:25

标签: asp.net entity-framework entity-framework-4.1

此代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;


    namespace ArticleLibrary_.NET.Models
    {
        public class ArticleLibraryEntitiy : DbContext
        {
            public DbSet<Post> Posts { get; set; }
            public DbSet<Person> People { get; set; }
            public DbSet<Tag> Tags { get; set; }
        }

      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {

      }

    }

错误地说:

Expected class, delegate, enum, interface, or struct

在行

protected override void OnModelCreating(ModelBuilder modelBuilder)

并且VS代码编辑器

也无法识别ModelBuilder

我是否必须添加任何其他参考?

1 个答案:

答案 0 :(得分:7)

您必须在类中编写OnModelCreating函数。你在它下面写下来。将您的代码更改为:

namespace ArticleLibrary_.NET.Models 
{ 
    public class ArticleLibraryEntitiy : DbContext 
    { 
        public DbSet<Post> Posts { get; set; } 
        public DbSet<Person> People { get; set; } 
        public DbSet<Tag> Tags { get; set; } 

        protected override void OnModelCreating(ModelBuilder modelBuilder) 
        { 

        } 
    } 

}