none
有关MVC3中code first下数据库创建不成功问题 RRS feed

  • 问题

  • 这是我的连接串

    <add name="MovieDBEntities"
        connectionString="Data Source=|DataDirectory|iMoviesDB.sdf"
        providerName="System.Data.SqlServerCe.4.0"/>

    MovieDBEntities是我存放数据库的,继承自DbContext,然后我在Global.asax里写:

    protected void Application_Start()
            {
                System.Data.Entity.Database.SetInitializer(new iMovies.Models.SampleData());
    simpledata是一个初始化我数据库的类,但我运行程序时发现我的数据库还是空的,请问这是怎么一回事
    2011年10月28日 15:24

全部回复

  • 第一次数据库查询的时候才创建

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年10月28日 16:23
    版主
  • iMovies.Models.SampleData你怎么写的?有没有重写Seed方法去填充一些实体类什么的?例如:

    public class BlogContextInitializer
    : DropCreateDatabaseIfModelChanges<BlogContext> { protected override void Seed(BlogContext context) { //create sample data and attach it to the context.
       new List<Blog>
       {
        new Blog { BloggerName = "Julie", Title = "My Code First Blog",
                   BlogDetail=new BlogDetails{Description="About code first",
    DateCreated=new System.DateTime(2011,3,1)} ,Posts=new List<Post>{
    new Post {
    Title="ForeignKeyAttribute Annotation",
    DateCreated=System.DateTime.Now,
    Content="Mark navigation property with ForeignKey"}} }, new Blog { BloggerName = "Ingemaar", Title = "My Life as a Blog", BlogDetail=new BlogDetails{
    Description="Random tidbits",
    DateCreated=new System.DateTime(2011,1,1)}}, new Blog { BloggerName = "Sampson", Title = "Tweeting for Dogs", BlogDetail=new BlogDetails{
    Description="How to tweet without opposable thumbs",
    DateCreated=new System.DateTime(2011,2,1)}} }.ForEach(b => context.Blogs.Add(b));
       base.Seed(context);  }
    }
    System.Data.Entity.Database.SetInitializer(new BlogContextInitializer());
    Refer From:http://thedatafarm.com/blog/data-access/seeding-a-database-with-ef4-code-first/

    如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处
    2011年11月4日 2:50