0

在查找了几分钟以了解为什么我的数据库没有播种后,我在 we.config 文件中发现了以下消息。

完整的消息列表如下:

消息 1 找不到元素“entityFramework”的架构信息。

消息 2 找不到元素“defaultConnectionFactory”的架构信息。

消息 3 找不到 at'type' 的架构信息。

现在我对 MVC 非常陌生,这是我构建的第一个应用程序。我想利用可用的 Code First 技术从我构建的模型中创建数据库。

这是我创建的模型的示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace TNTMVC.Models.UECDECZA.BaseTables
{
public class Actions
{
    [Key]
    public int ActionID { get; set; }
    [Timestamp]
    public DateTime ActionDateCreated { get; set; }
    [Required]
    [DefaultValue(true)]
    public bool ActionActive { get; set; }
    [Required]
    public string ActionName { get; set; }
    [Required]
    [DefaultValue(true)]
    public bool ActionRequiresComponents { get; set; }
    [Required]
    [DefaultValue(1000)]
    public int ActionListOrder { get; set; }
}
}

然后我创建了我的数据库上下文类,看起来像这样。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using TNTMVC.Models.UECDECZA.BaseTables;
using TNTMVC.Models.UECDECZA.DataTables;


namespace TNTMVC.Models.UECDECZA
{
public class UecdeczaContext : DbContext
{
    public DbSet<Actions> Actions { get; set; }
    public DbSet<Application> Application { get; set; }
    public DbSet<Area> Area { get; set; }
 }
 }

这是我的 Web.Config 文件:

<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=152368
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit   http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"   type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="UecdeczaContext" connectionString="Data Source=.\SQLExpress;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  </assemblies>
  </compilation>
  <authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
  </authentication>
  <membership>
  <providers>
    <clear />
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
   </providers>
  </membership>
  <profile>
  <providers>
    <clear />
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
   </providers>
  </profile>
  <roleManager enabled="false">
  <providers>
    <clear />
    <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
    </providers>
  </roleManager>
  <pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
    <add namespace="System.Data.Entity" />
  </namespaces>
  </pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
   <parameters>
    <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True;      MultipleActiveResultSets=True" />
    </parameters>
 </defaultConnectionFactory>
 </entityFramework>
</configuration>

这应该是一个成功的模型实现吧?但它似乎根本不起作用。它没有播种我的数据库,也没有工作:(

我也在 ASP 论坛上问过同样的问题:我的 ASP 论坛问题

4

1 回答 1

0

您似乎没有调用 Seed() 方法的初始化程序(默认初始化程序除外)。有关更多信息,请参阅 Julia Lerman关于播种的博客文章。你需要类似的东西:

public class UecdeczaInitializer : DropCreateDatabaseIfModelChanges<UecdeczaContext >
{
    protected override void Seed(UecdeczaContext context)
    {
        new List<Actions>
        {
            new Actions
                {
                    ActionName = "My action name"
                }
            }
        }.ForEach(b => context.Blogs.Add(b));
       base.Seed(context);  }
}

您可能需要在其中添加更多属性来为您的 Actions 实体播种,但这是基本思想。

于 2012-08-20T11:25:02.020 回答