2012-02-13 144 views
0

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

我是新来的参数,所以要耐心......我已经第一次创建了数据库,似乎都是对的;但我没有创建一个DropCreateDatabaseIfModelChanges方法来更改表格,我决定手动删除数据库。

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

我已经实现了初始化,它是在从上下文不同类...

public class WidgetDbInitializer : DropCreateDatabaseIfModelChanges<WidgetDbContext> 
    { 
} 

设置好的它在Global.asax.cs中,被迫init,可以

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: 
+1

数据库将只会在您的应用程序中做一些事情来创建,以便访问它。如果您编写一些试图访问它的代码,然后运行它,会发生什么?如果数据库没有被创建,你必须得到一个错误,对吧? – 2012-02-13 12:19:47

+0

是的,每次我尝试做某件事时,例如CREATEIFNOTEXIST -.-我得到一个异常错误,参数KEY不能为空。这对于我尝试对数据库执行的每个操作都是如此。 database.mdf仍然不会在sqlexpress的数据文件夹中创建,也不会在我的应用程序的app_data中创建,它只是不存在 – doompro 2012-02-13 15:51:28

+0

添加您用于访问数据库的代码以及因此导致的错误。 – 2012-02-13 15:59:18

回答

0

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

protected override void Seed(WidgetDbContext context) 
{ 

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

首先检查您的覆盖种子正确调用,然后如果您的数据库剂量不产生,你会得到一个异常。

+0

我已经尝试过了,每当我尝试访问数据库时,我都会得到相同的错误:ArgumentNullException,参数“key”不能为null。数据库仍然没有创建;要么当没有方法的参数键,要么当我传递一个key属性不为null的元素来插入数据库 – doompro 2012-02-13 16:30:31

+0

当你得到错误时分享你的堆栈跟踪 – MHF 2012-02-13 16:39:56

+0

> Portale.DLL!Portale.Models.WidgetDbInitializer.Init( Portale.Models。WidgetDbContext上下文)第15行\t > Portale.DLL!Portale.MvcApplication.Application_Start()第47行+ 0xd字节 – doompro 2012-02-13 18:03:46

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; } 
} 

数据库只是不接受型“类型”,精致只要我删除了该领域所有的工作......我只是不看它,因为它正在使用“对象”类型,没想到它没有与类型..