首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据库初始值设定项未设定种子

问数据库初始值设定项未设定种子
EN

Stack Overflow用户
提问于 2015-04-19 16:03:06
回答 2查看 331关注 0票数 2

我一直在寻找一个答案,有很多个答案,但我无法从解决这些问题的解决方案中找到答案。我的问题是数据没有被植入数据库。数据库工作,表被创建,但是来自数据库初始值设定项的数据没有被放入其中。

上下文类

代码语言:javascript
复制
using System.Data.Entity;

namespace FlextrafikWeb.Models
{
    public class Context : DbContext
    {
        public Context() : base("FlextrafikWeb") { }

        public DbSet<Car> Cars { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Location> Locations { get; set; }
        public DbSet<Price> Prices { get; set; }
    }

}

数据库初始化器

代码语言:javascript
复制
using System.Collections.Generic;
using System.Data.Entity;

namespace FlextrafikWeb.Models
{

    public class DatabaseInitializer :     DropCreateDatabaseIfModelChanges<Context>
    {
        protected override void Seed(Context context)

        {
            GetUsers().ForEach(u => context.Users.Add(u));
            GetCars().ForEach(c => context.Cars.Add(c));
            GetLocations().ForEach(l => context.Locations.Add(l));
            GetPrices().ForEach(p => context.Prices.Add(p));

            context.SaveChanges();
        }

        private static List<Car> GetCars()
        {
            var cars = new List<Car>
        {
            new Car
            {
                CarID = 1,
                GaranteeCarNumber = 2,
                RegistrationNumber = 123456,
                ContactTelephone = "12 34 56 78",
                CarType = 1,
                CarLocation = null,
                CarPrice = null,
                CarOwner = null,
                UserID = 1,
            },

            new Car
            {
                CarID = 3,
                GaranteeCarNumber = 4,
                RegistrationNumber = 654321,
                ContactTelephone = "87 65 43 21",
                CarType = 2,
                CarLocation = null,
                CarPrice = null,
                CarOwner = null,
                UserID = 2,
            },

        };

            return cars;
        }

        private static List<User> GetUsers()
        {
            var users = new List<User>
        {
            new User
            {
                UserID = 1,
                UserName = "Firma A",
                CompanyCVR = 12345678
            },

            new User
            {
                UserID = 2,
                UserName = "Firma B",
                CompanyCVR = 98765432
            }
        };

            return users;
        }

        private static List<Location> GetLocations()
        {
            var locations = new List<Location>
        {
            new Location
            {
                LocationID = 1,
                StreetName = "Testgade",
                StreetNumber = 4,
                PostalCode = 5000,
                City = "Odense",
                Municipality = "Odense",
            },

              new Location
            {
                LocationID = 2,
                StreetName = "Testvej",
                StreetNumber = 34,
                PostalCode = 5300,
                City = "Kerteminde",
                Municipality = "Kerteminde",
            }
        };

            return locations;
        }

        private static List<Price> GetPrices()
        {
            var prices = new List<Price>
        {
            new Price
            {
                PriceID = 1,
                StartupFeeWeekday = 30.00,
                RunningRateWeekday = 330.00,
                WaitingRateWeekday = 250.00,
                StartupFeeEvening = 60.00,
                RunningRateEvening = 500.00,
                WaitingRateEvening = 400.00,
                StartupFeeWeekend = 600.00,
                RunningRateWeekend = 500.00,
                WaitingRateWeekend = 400.00,
            },

                new Price
            {
                PriceID = 2,
                StartupFeeWeekday = 10.00,
                RunningRateWeekday = 249.00,
                WaitingRateWeekday = 249.00,
                StartupFeeEvening = 20.00,
                RunningRateEvening = 269.00,
                WaitingRateEvening = 269.00,
                StartupFeeWeekend = 20.00,
                RunningRateWeekend = 269.00,
                WaitingRateWeekend = 269.00,
            },
        };

            return prices;

        }
    }
}

全局

代码语言:javascript
复制
    using System.Web.Security;
using System.Web.SessionState;
using System.Data.Entity;
using FlextrafikWeb.Models;

namespace FlextrafikWeb
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // Initialize the product database.
            Database.SetInitializer(new DatabaseInitializer());


        }
    }
}

我不知道你还需要什么,但我似乎什么都想不出来。

EN

回答 2

Stack Overflow用户

发布于 2015-04-19 16:44:03

这里有两件事:

  1. 数据库初始化程序仅当数据库重新创建时,在您的情况下,初始化程序将在数据模型更改时启动,要始终重新创建和设定数据库种子,请将您的初始化程序更改为继承dropcreate数据库始终初始化程序。
  2. 您需要在dbcontect构造函数(很可能是通过静态构造函数)

中专门设置初始化器

本教程将为您提供更多详细信息http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx

希望这能有所帮助

票数 2
EN

Stack Overflow用户

发布于 2015-04-19 16:40:10

您应该在context类的构造函数中设置自定义初始值设定项的使用:

代码语言:javascript
复制
using System.Data.Entity;

namespace FlextrafikWeb.Models
{
    public class Context : DbContext
    {
        public Context() : base("FlextrafikWeb")
        {
            Database.SetInitializer(new DatabaseInitializer());
        }

        public DbSet<Car> Cars { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Location> Locations { get; set; }
        public DbSet<Price> Prices { get; set; }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29727350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档