c# - Customization and integration of own database with mvc5 identity -
i'm working on mvc project required authentication , authorization created using identity, project database includes main entities should use system, best practice deal situation.
- should customize identity applicationuser requirements.
- create own tables (models) , integrate them identity tables , don't know how that. or other solutions.
you should of course customize identity entities, whole reason creation of identity: allow greater extensibility. have different types of "users", should inherit applicationuser
; importantly, not identityuser
directly. assure core identity relationships (roles, claims, logins, etc.) tied single "user" table, while can either extend table or create other tables hold additional user data.
public class applicationuser : identityuser public class student : applicationuser public class instructor : applicationuser
by default, inheritance implemented tph (table per hierarchy), known sti (single table inheritance). means properties derived classes represented columns on single database table. discriminator
column added, hold name of actual class saved, i.e. "applicationuser", "student", or "instructor". ef use column when building object graphs queries instantiate right "user" type.
there's pros , cons approach. since exists in single table, queries simple , quick. however, approach necessitates properties on each derived class must nullable at database level. obvious reason why because if instructor
has required column, not able save student
, since student
not have property fulfill requirement. can still enforce properties required @ view level, using view models. actual column in database must nullable, though.
an alternative approach use what's called tpt (table per type). in inheritance strategy, table created base class (applicationuser
) common properties. then, table created each discreet derived class, properties exist on class. foreign key added table base class, ef use join common data table specific data on derived class' table. approach allows enforce not null @ database level, of course requires join bring in data, can slow down queries.
to implement tpt, need add [table]
annotation derived class:
[table("students")] public class student : applicationuser [table("instructors")] public class instructor : applicationuser
one final thing of note how you'll need utilize usermanager
. if scaffolded accountcontroller
, you'll notice sets usermanager
controller property, utilized create users, lookup users, change passwords, etc. instance of usermanager<applicationuser>
, it's generic type. if need work student
or instructor
, you'll need instantiate usermanager<student>
, usermanager<instructor>
, respectively. can't use instance of usermanager<applicationuser>
upcast derived type applicationuser
. example:
var student = new student { ... }; await usermanager.createasync(student);
would result in applicationuser
being saved database. student-specific data discarded , discriminator
column's value "applicationuser".
Comments
Post a Comment