我正在使用 mysql 驱动程序 6.6.4 和 EF 4.4.0.0。我的上下文可以创建数据库,它还创建了一个 _MigrationHistory 表,我可以插入、列出、删除记录。
我可以使用创建模型添加一个动作,并自动创建模型,一切都很完美这里是我的清晰上下文
public class MyContext : DbContext
{
static MyContext()
{
Database.SetInitializer<MyContext>(null);
}
public MyContext()
: base("Name=MyContext")
{
}
public DbSet<AdminUser> AdminUsers { get; set; }
public DbSet<Feedback> Feedbacks { get; set; }
public DbSet<Navigation> Navigations { get; set; }
public DbSet<SiteLink> SiteLinks { get; set; }
public DbSet<SiteNew> SiteNews { get; set; }
public DbSet<StockList> StockLists { get; set; }
public DbSet<SubNavigation> SubNavigations { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new AdminUserMap());
modelBuilder.Configurations.Add(new FeedbackMap());
modelBuilder.Configurations.Add(new NavigationMap());
modelBuilder.Configurations.Add(new SiteLinkMap());
modelBuilder.Configurations.Add(new SiteNewMap());
modelBuilder.Configurations.Add(new StockListMap());
modelBuilder.Configurations.Add(new SubNavigationMap());
}
我的 constring 与我的上下文同名,正如所说的那样,这一切都有效。我还将数据提供程序添加到我的 web.config 文件中。
我什至在创建存储过程中添加了一些代码,这一切都很好,底线一切看起来都很好,就我而言,我已经完成了我可能做的所有事情。
但是当我创建一个控制器时,我得到了这个:[使用带有创建、编辑、删除选项的创建控制器)
现在这里和网络上有很多关于这个的噪音,但我实际上已经涵盖了所有内容。我什至将我的 dbContext 设置为 DbSets,仅此而已。这已经让我头疼了很长一段时间,我已经做了所有的事情并阅读了所有可能的内容,然后才开始在这里提问。
现在我知道使用 SQL Server 等可能会容易得多,但是我没有那个选项,我有一个非常好的 mysql 集群和许多首先使用 EF 和代码的站点,当我安装 asp.net MVC 时问题似乎开始了4 并升级到最新的.net。
请有任何想法,我会尝试任何东西
网络配置
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<connectionStrings>
<add name="MyContext" connectionString="server=localhost;database=MyDB;user id=myuser; password=mypass; Pooling=false" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<pages controlRenderingCompatibilityVersion="4.0" clientIDMode="AutoID">
<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" />
</namespaces>
</pages>
</system.web>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.6.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.6.4.0" newVersion="6.6.4.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
模型:
public class AdminUser
{
public int Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public short UserLevel { get; set; }
}
映射
public class AdminUserMap : EntityTypeConfiguration<AdminUser>
{
public AdminUserMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.UserName)
.IsRequired()
.HasMaxLength(50);
this.Property(t => t.Password)
.IsRequired()
.HasMaxLength(50);
this.Property(t => t.Email)
.HasMaxLength(150);
// Table & Column Mappings
this.ToTable("AdminUsers", "eyebag");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.UserName).HasColumnName("UserName");
this.Property(t => t.Password).HasColumnName("Password");
this.Property(t => t.Email).HasColumnName("Email");
this.Property(t => t.UserLevel).HasColumnName("UserLevel");
}
}