i'm using existing database code first approach.
i using new tables in db except 1 existing table need make use of. want able read table , not have changes occur if call update-database. how go this?
if use code below gets totally excluded context if remove ignore line in onmodelcreating have issues update-database command exists in db(and mentioned, not want update table @ time )
public class dbcontext : identitydbcontext<applicationuser> { public dbcontext() : base("defaultconnection", throwifv1schema: false) { } public dbset<person> persons { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); modelbuilder.conventions.remove<pluralizingtablenameconvention>(); modelbuilder.ignore<person>(); } public static dbcontext create() { return new dbcontext(); } }
i changed code following
public class dbcontext : identitydbcontext<applicationuser> { public dbcontext() : base("defaultconnection", throwifv1schema: false) { } //public dbset<person> persons { get; set; } public dbquery<person> persons { { // don't track changes query results return set<person>().asnotracking(); } } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); modelbuilder.conventions.remove<pluralizingtablenameconvention>(); modelbuilder.entity<person>(); } public static dbcontext create() { return new dbcontext(); }
}
*note migrations enabled @ point
then got error "there object named 'person' in database."
then run pm> add-migration firstmigration
after go , delete create , drop table in migration script
run pm> update-database , should work now.
i fixed of using information article: https://blog.rajsoftware.com/2014/07/12/entity-framework-code-first-automatic-migration-existing-tables/
Comments
Post a Comment