mysqlドライバー6.6.4およびEF4.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だけに設定しました。これはかなり長い間私の頭を燃やしてきました、そして私はここで尋ねる動きをする前にすべてをしてそして可能なすべてを読みました。
これはSQLServerなどでおそらくはるかに簡単であることがわかりましたが、そのオプションはありません。非常に優れたmysqlクラスターがあり、最初にEFとコードを使用する多くのサイトがあり、asp.netMVCをインストールすると問題が発生するようです。 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");
}
}