EF 4.1 升级后代码首次问题

发布于 2024-10-25 12:19:10 字数 1068 浏览 1 评论 0原文

好的,我刚刚通过 NuGet 升级到 EF Code First 4.1,现在我的 JobSiteContext.cs 类中出现以下构建错误:

“名称 'DbDatabase' 在当前上下文中不存在”

这里是我的代码:

public class JobSiteContext : DbContext
{
    public DbSet<JobSite.Models.Job> Jobs { get; set; }

    public DbSet<JobSite.Models.Location> Locations { get; set; }

    public DbSet<JobSite.Models.Profile> Profiles { get; set; }

    public JobSiteContext()
    {
        // Instructions:
        //  * You can add custom code to this file. Changes will *not* be lost when you re-run the scaffolder.
        //  * If you want to regenerate the file totally, delete it and then re-run the scaffolder.
        //  * You can delete these comments if you wish
        //  * If you want Entity Framework to drop and regenerate your database automatically whenever you 
        //    change your model schema, uncomment the following line:
            DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges<JobSiteContext>());
    }
}

任何人都可以指出我正确的方向吗?

谢谢保罗

Okay, so I just upgraded thru NuGet to EF Code First 4.1 and now I get the following build error within my JobSiteContext.cs class:

"The name 'DbDatabase' does not exist in the current context"

Here is my code:

public class JobSiteContext : DbContext
{
    public DbSet<JobSite.Models.Job> Jobs { get; set; }

    public DbSet<JobSite.Models.Location> Locations { get; set; }

    public DbSet<JobSite.Models.Profile> Profiles { get; set; }

    public JobSiteContext()
    {
        // Instructions:
        //  * You can add custom code to this file. Changes will *not* be lost when you re-run the scaffolder.
        //  * If you want to regenerate the file totally, delete it and then re-run the scaffolder.
        //  * You can delete these comments if you wish
        //  * If you want Entity Framework to drop and regenerate your database automatically whenever you 
        //    change your model schema, uncomment the following line:
            DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges<JobSiteContext>());
    }
}

Can anyone point me in the right direction?

Thanks Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2024-11-01 12:19:10
public class Initializer : IDatabaseInitializer<AuthenticationContext>
        {
            public void InitializeDatabase(AuthenticationContext context)
            {
                if (context.Database.Exists() && !context.Database.CompatibleWithModel(false))
                    context.Database.Delete();

                if (!context.Database.Exists())
                {
                    context.Database.Create();

                }
            }
        }
public class Initializer : IDatabaseInitializer<AuthenticationContext>
        {
            public void InitializeDatabase(AuthenticationContext context)
            {
                if (context.Database.Exists() && !context.Database.CompatibleWithModel(false))
                    context.Database.Delete();

                if (!context.Database.Exists())
                {
                    context.Database.Create();

                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文