在WPF中是否可以使用RecreateDatabaseIfModelChanges?

时间:2011-03-31 06:29:01

标签: wpf entity-framework ef-code-first entity-framework-4.1

我正在尝试实体框架代码优先。我似乎无法在WPF 4.0中找到用于RecreateDatabaseIfModelChanges的程序集/命名空间。这是仅限ASP.NET的功能吗?如果没有,我应该参考哪个装配?

这是我的代码:

using System;
using System.Data.Entity;
using System.Windows;
using CodeFirstTester.Models;

namespace CodeFirstTester
{
    public partial class App : Application
    {
        static App()
        {
            // this fails:
            Database.SetInitializer(new RecreateDatabaseIfModelChanges<NerdDinners>());

            // The type or namespace name 'RecreateDatabaseIfModelChanges'
            // could not be found (are you missing a using directive or
            // an assembly reference?)                

            using (var nerdDinners = new NerdDinners())
            {
                var dinner = new Dinner()
                {
                    Title = "Party at Scott's House",
                    EventDate = DateTime.Parse("12/31/2010"),
                    Address = "Building 40",
                    HostedBy = "scottgu@microsoft.com"
                };
                nerdDinners.Dinners.Add(dinner);
                nerdDinners.SaveChanges();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:51)

初始值设定项称为DropCreateDatabaseIfModelChanges。它可以在System.Data.Entity命名空间中的EntityFramework.dll(EF 4.1)中找到。