我正在使用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。
任何想法,我会尝试任何事情
的web.config
<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");
}
}
答案 0 :(得分:0)
试试这些: 也许默认的端口号在某种程度上是错误的,所以将端口号添加到您的连接字符串以匹配您的服务器设置。 将您的设置减少到最小值,删除不正常运行所需的任何内容:appSettings,运行时(不确定这是否会正确默认),处理程序(在system.webserver内)。
答案 1 :(得分:0)