c# - Entity Framework 7 migration not creating tables -
i working first project using entity framework 7 , connecting sql server database created there no tables in yet. have created dbcontext
, created class, set dbset<>
inside context. ran commands enable migrations , create first migration, rand command update database. looked work fine, no errors came up, when @ database efmigraitonshistory
table created. when @ class created initial migration blank. doing wrong?
commands running:
dnvm install latest -r coreclr dnx ef migrations add myfirstmigration dnx ef database update
context:
namespace jobsight.dal { public class jobsightdbcontext : dbcontext { public dbset<navigationmenu> navigationmenu { get; set; } } }
table class:
namespace jobsight.dal { public class navigationmenu { [required, databasegenerated(databasegeneratedoption.identity)] public int16 id { get; set; } public string controllername { get; set; } public string actionname { get; set; } public string externalurl { get; set; } public string title { get; set; } public int16? parentid { get; set; } public virtual navigationmenu parent { get; set; } } }
startup.cs:
public void configureservices(iservicecollection services) { services.addmvc(); services.addentityframework() .addsqlserver() .adddbcontext<jobsightdbcontext>(options => { options.usesqlserver(configuration["data:jobsightdatabase:connectionstring"]); }); }
class initial migration (autogenerated ef):
namespace jobsight.webui.migrations { public partial class initial : migration { protected override void up(migrationbuilder migrationbuilder) { } protected override void down(migrationbuilder migrationbuilder) { } } }
edit: after doing poke has suggested new auto-generated migration. table still not being created @ database level though.
namespace jobsight.webui.migrations { public partial class myfirstmigration : migration { protected override void up(migrationbuilder migrationbuilder) { migrationbuilder.createtable( name: "navigationmenu", columns: table => new { id = table.column<short>(nullable: false) .annotation("sqlserver:valuegenerationstrategy", sqlservervaluegenerationstrategy.identitycolumn), actionname = table.column<string>(nullable: true), controllername = table.column<string>(nullable: true), externalurl = table.column<string>(nullable: true), parentid = table.column<short>(nullable: true), title = table.column<string>(nullable: true) }, constraints: table => { table.primarykey("pk_navigationmenu", x => x.id); table.foreignkey( name: "fk_navigationmenu_navigationmenu_parentid", column: x => x.parentid, principaltable: "navigationmenu", principalcolumn: "id", ondelete: referentialaction.restrict); }); } protected override void down(migrationbuilder migrationbuilder) { migrationbuilder.droptable("navigationmenu"); } } }
you need set entity in database context first. @ least, need this:
protected override void onmodelcreating(modelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); modelbuilder.entity<navigationmenu>(); }
the problem migrations bit hidden in project layout. have jobsight.dal
project contains entities , database context. , have project jobsight.webui
actual asp project containing startup.cs
database setup.
this causing problems because default ef assume find in current assembly. if launching ef
command web project, create migrations in there if context in project. when you’re trying apply migration, ef not find since in context’s project.
so fix this, need create migrations in dal
project. can specifying project when call ef
command:
dnx ef migrations add example -p jobsight.dal
you can verify worked running dnx ef migrations list
afterwards. should return example
migration; previously, command didn’t return anything: not find migration reason why update
command said done
(without applying migration) , database wasn’t created. if migration there, can apply using:
dnx ef database update
note since migration created in dal project, need add reference entityframework.microsoftsqlserver
there, otherwise project not compile. need before can run list
command above.
finally, more information this, see this issue.
Comments
Post a Comment