2012-02-13 141 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; } 
} 

數據庫只是不接受型「類型」,精緻只要我刪除了該領域所有的工作......我只是不看它,因爲它正在使用「對象」類型,沒想到它沒有與類型..