none
DropCreateDatabaseIfModelChanges 加初始数据,异常 RRS feed

  • 问题

  •  

    先贴我的代码

     

    我需要在模型改变的时候,重新创建数据库,并加入初始数据。。。。

     

            protected void Application_Start()
            {
                Database.SetInitializer<ServicesEntities>(new ServicesEntitiesIntializer());
     
                AreaRegistration.RegisterAllAreas();
                RegisterRoutes(RouteTable.Routes);          
     
            }

     

        public class ServicesEntitiesIntializer : DropCreateDatabaseIfModelChanges<ServicesEntities>
        {
            protected override void Seed(ServicesEntities context)
            {
                //base.Seed(context);
     
                var Categories = new List<Categories>{
                    new Categories{
                        Description = "guest"
                    }
                };
     
                Categories.ForEach(d => context.Categories.Add(d));
     
                var Permissions = new List<Permissions>{
                    new Permissions{
                        Category_ID = 1,
                        Description="guest"
                    }
                };
     
                Permissions.ForEach(d => context.Permissions.Add(d));

    这段代码是ServicesEntities在类中,继承DbContext

    ..........
            public DbSet<Structure> Structure { getset; }         protected override void OnModelCreating(DbModelBuilder modelBuilder)         {             //base.OnModelCreating(modelBuilder);             modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); ///去掉EdmMetedata表,报下面的错误...             modelBuilder.Configurations.Add(new CategoriesMap());             modelBuilder.Configurations.Add(new PermissionsMap());             modelBuilder.Configurations.Add(new MenusMap());


    ..........

    运行的时候报这个错误

    错误信息

    Model compatibility cannot be checked because the EdmMetadata type was not included in the model. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.


    当我不去掉edmmetadata表时,提示这个错误信息

     

    这段代码是ServicesEntities在类中,继承DbContext

    ..........
            public DbSet<Structure> Structure { getset; }         protected override void OnModelCreating(DbModelBuilder modelBuilder)         {             //base.OnModelCreating(modelBuilder);             //modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); ///不去掉EdmMetedata表,报下面的错误...             modelBuilder.Configurations.Add(new CategoriesMap());             modelBuilder.Configurations.Add(new PermissionsMap());             modelBuilder.Configurations.Add(new MenusMap());


    ..........

     

    错误信息:

    Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.

     

    我使用的是  Entity Framework 4.1    mvc 2.0

     

    当我不注册

    Database.SetInitializer<ServicesEntities>(new ServicesEntitiesIntializer());
    

      这个对象,则是正常的。

     

    各位大侠帮帮看看,是怎么回事呢?


    2011年6月16日 11:31

答案

全部回复