ASP. NET MVC + mysqlで open SOUEL environment structure

Source: Internet
Author: User

from:http://qiita.com/midori44/items/ef7cdd1d37c353e44b5f

ASP & entityframeworkによるコードファースト opened Souel をmysqlで structure してみます.
と said っても this time はプロジェクトを made into してasp.net identityによるユーザー recognized をmysqlで motion かすところまで.

(2015.10.22 track)
The Visual Studio +. NET Framework 4.6でも to confirm.
Now in beta 8が のasp.net 5 (ASP. 6) は, まだmysql.data.entityが 's really into 対応 していないのでmysqlは use できません.
(Remember ここまで)

Ready

Visual Studio communityまたはprofessional above intent.
The Express for webでは Hiroki Machine can が えないので attention.

プロジェクト made into

The new rules are->プロジェクト-> Visual C #--and ASP. Webアプリケーションを Select Drivers.
The. NET Framework 4, 4.5および4.5.1のサポートは2016 January 12 occupies finally するらしいので.net Framework 4.5.2を push policies.

テンプレートはmvcを Choose Drivers 前転して開脚座り, recognize に"ユーザーアカウント"を use.
This time は"single body テストの added"とazureの"クラウド inside のホスト"のチェックは outside しておきます.

これでasp.netアプリケーションが new rules made into されました.
デバッグ Natsumi Sato Line すれば, ユーザー registration? ログインといったasp.net identityの to confirm できることが with できます.

MySQL Connect Yobitsugi

デフォルトではsql Server Compactが uses されているので, then Yobitsugi をmysqlに Slew.

mysql.data.entityのインストール? Setting

"nugetパッケージの Management"からmysqlで Eiken 前転して開脚座り MySql.Data.Entity をインストールします (2015/4/14, the latest version of は6.9.6).
ついでにmicrosoft ASP. Mvcとentityframeworkも The latest version of にアップデートしておきます.

Second occupies Web.config きます (viewsディレクトリ Web.config ではなくプロジェクト straight down のほう).
entityFrameworkおよび system.data set は のインストール occupies で されているので, のみ slew MySql.Data.Entity connectionStrings more します.

Web. config
<connectionStrings>    <!--<add name= "defaultconnection" connectionstring= "Data source= (LocalDb) \v11.0; attachdbfilename=| Datadirectory|\aspnet-webapp-****.mdf;initial Catalog=aspnet-webapp-****.;i ntegrated security=true "providername=" System.Data.SqlClient "/>-    name= connectionstring=  Providername=/></connectionStrings>     

ここではxampp on the で initial setting のmysqlを want to set していますので, then yobitsugi text column のパラメータは environment occupies and わせて slew more してください.

マイグレーション

Yobitsugi 上付いて, マイグレーションを Natsumi Sato 前転して開脚座り で to use されるテーブルをコードから auto generation します.

enable-migrations

"パッケージマネージャーコンソール"から Times のコマンドを Natsumi Sato line します.
Enable-Migrations

Migrationディレクトリと Configuration.cs ファイルが made into されました.

にadd-migrationを Natsumi Sato Line するのですが, このまま Natsumi Sato line すると The following のエラーが Souel Sheng してしまいます.

プロバイダー ‘MySql.Data.MySqlClient‘ で MigrationSqlGenerator が見つかりませんでした。対象の移行構成クラスで SetSqlGenerator メソッドを使用して、追加の SQL ジェネレーターを登録してください。

なので first ほどお generate された Configuration.cs occupies, Buchholz to クラスを DbConfiguration add してmigrationsqlgeneratorを Login registration します.

Configuration.cs
UsingMySql.Data.EntityInternalSealedClassConfiguration: dbmigrationsconfiguration< Applicationdbcontext>{ ... public class mysqlconfiguration : dbconfiguration {public mysqlconfiguration () {setmigrationsqlgenerator ( Mysqlproviderinvariantname. Providername () => new  Mysqlmigrationsqlgenerator ()); } }}          /span>                

これでadd-migrationが Natsumi Sato Line できるようになりました.

add-migration

"パッケージマネージャーコンソール"から Times のコマンドを Natsumi Sato line します.
Add-Migration Initial

Migrationディレクトリ inside occupies 2015****_Initial.cs が made into されました.
このファイルにコードから generate されたテーブルのカラム が save されています.

Finally にupdate-databaseを Natsumi Sato line するのですが, このまま Natsumi Sato line すると below のエラーが Souel sheng してしまいます.

Specified key was too long; max key length is 767 bytes

これはmysqlではキーの さの Upper Limit が255 (Utf-8のvarchar type) のためなので and キーとなるカラムの さを256から255に correction します.

2015****_initial.cs
PublicOverridevoidUp(){CreateTable("dbo. Aspnetroles ",C=New{Id=C.String(Nullable:False,MaxLength:128,Storetype:"Nvarchar"),MAXLENGTH:256-255Name=C.String(Nullable:False,MaxLength:255,Storetype:"Nvarchar"),}).PrimaryKey(T=T.Id).Index(T=T.Name,Unique:True,Name:"Rolenameindex");...CreateTable("dbo. Aspnetusers ",C=New{Id=C.String(Nullable:False,MaxLength:128,Storetype:"Nvarchar"),Email=C.String(MaxLength:256,Storetype:"Nvarchar"),Emailconfirmed=C.Boolean(Nullable:False),PasswordHash=C.String(Unicode:False),Securitystamp=C.String(Unicode:False),PhoneNumber=C.String(Unicode:False),Phonenumberconfirmed=C.Boolean(Nullable:False),Twofactorenabled=C.Boolean(Nullable:False),Lockoutenddateutc=C.Datetime(Precision:0),Lockoutenabled=C.Boolean(Nullable:False),Accessfailedcount=C.Int(Nullable:False),MAXLENGTH:256-255UserName=C.String(Nullable:False,maxlength: 255storetype :  "nvarchar" }) . Primarykey (t => t. Id) . Index (t => t. Usernameunique: truename:  "Usernameindex" );  ...                /span>                

Moo, マイグレーション to maintain able __migrationhistory テーブルも correction able necessary があります.
First ほどの Configuration.cs occupies HistoryContext Buchholz クラスを make してキーの さを slew more します.

Configuration.cs
PublicClassMysqlconfiguration:Dbconfiguration{PublicMysqlconfiguration(){Setmigrationsqlgenerator(Mysqlproviderinvariantname.ProviderName,()=NewMysqlmigrationsqlgenerator());Sethistorycontext("MySql.Data.MySqlClient",(DbConnection,Defaultschema)=NewMysqlhistorycontext(DbConnection,Defaultschema));}PublicClassMysqlhistorycontext:Historycontext{PublicMysqlhistorycontext(DbConnectionDbConnection,StringDefaultschema):Base(DbConnection,Defaultschema){}ProtectedOverridevoidOnmodelcreating(DbmodelbuilderModelBuilder){Base.Onmodelcreating(ModelBuilder);ModelBuilder.Entity<Historyrow> ().Property (h => h. Migrationid). hasmaxlength (255).  Isrequired (); modelbuilder. Entity<historyrow> ().  Property (h => h. Contextkey). hasmaxlength (255).  Isrequired (); } }}          /span>                

これでupdate-databaseが Natsumi Sato Line できるようになりました.

Update-database

"パッケージマネージャーコンソール"から Times のコマンドを Natsumi Sato line します.
Update-Database

The above でmysqlにデータベースが generates されます.

デバッグ Natsumi Sato してユーザー registration すれば テーブルが aspnetusers update されることが confirm できます.

ASP. NET MVC + mysqlで open SOUEL environment structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.