实体框架不会创建数据库

时间:2012-02-13 11:59:16

标签: asp.net-mvc asp.net-mvc-3 entity-framework

我正在mvc asp.net应用程序中使用实体框架创建一个数据库,首先使用代码。

我对这个论点不熟悉所以请耐心等待......我第一次创建数据库,一切似乎都是正确的;但我没有创建DropCreateDatabaseIfModelChanges方法来更改表我决定手动删除数据库。

问题是数据库没有重新创建!

我已经实现了初始化程序,它与上下文不同...

public class WidgetDbInitializer : DropCreateDatabaseIfModelChanges<WidgetDbContext>
    {
}

在Global.asax.cs中设置它并强制初始化

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        Database.SetInitializer<Portale.Models.WidgetDbContext>(new Portale.Models.WidgetDbInitializer());
        var _initer = new WidgetDbInitializer();

        using (var db = new WidgetDbContext())
        {
            _initer.Seedit(db);
            db.Database.Initialize(true);
        }
    }

我只是默认的连接字符串,现在我不关心它......

请帮助我,我通过网络阅读了大量的文章,我无法得到解决方案......

我得到的错误:

System.ArgumentNullException non è stata gestita dal codice utente
  Message=Il valore non può essere null.
Nome parametro: key
  Source=mscorlib
  ParamName=key
  StackTrace:
       in System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
       in System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
       in System.Data.Entity.ModelConfiguration.Configuration.Mapping.SortedEntityTypeIndex.Add(EdmEntitySet entitySet, EdmEntityType entityType)
       in System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Analyze()
       in System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Configure()
       in System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
       in System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
       in System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
       in System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
       in System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       in System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
       in System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       in System.Data.Entity.Internal.InternalContext.Initialize()
       in System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       in System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       in System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
       in System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
       in System.Linq.Queryable.Join[TOuter,TInner,TKey,TResult](IQueryable`1 outer, IEnumerable`1 inner, Expression`1 outerKeySelector, Expression`1 innerKeySelector, Expression`1 resultSelector)
       in Portale.Controllers.WidgetContainerController.Index() in C:\Users\doompro\Documents\Visual Studio 2010\Projects\Portale\Portale\Controllers\WidgetContainerController.cs:riga 56
       in lambda_method(Closure , ControllerBase , Object[] )
       in System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       in System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       in System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       in System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       in System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 

2 个答案:

答案 0 :(得分:0)

覆盖WidgetDbInitializer类中的种子功能,并尝试将一些数据添加到数据库中。

protected override void Seed(WidgetDbContext context)
{

context.yourodel.add(new class() )
}

首先检查您的覆盖种子调用是否正确,然后如果您的数据库未生成,则会出现异常。

答案 1 :(得分:0)

解决了问题:

public class Widget
{
    //This properties rapresent the primary key for entity framework
    [Key]
    public int WidgetID { get; set; }
    //Foreing key to the column where this widget is stored
    public virtual int ColumnID { get; set; }
    //The title of the widget
    public string Title { get; set; }
    //Controller of the Widget, this property may be used on the RenderAction call
    public string Controller { get; set; }
    //ActionMethod of the Widget, this property may be used on the RenderAction call
    public string ActionMethod { get; set; }
    //The Type of the Model, used on deserialization
    public Type ModelType { get; set; }
    //The context of the widget
    public string SerializedModel { get; set; }
}

数据库只是没有接受类型&#34; Type&#34;,一旦我删除了该字段,一切正常......我只是没有看到它,因为它正在工作使用&#34;对象&#34;类型,没想到它不适用于类型..