c# - How to store Active Directory user GUID using Entity Framework? -
i have site authenticates using active directory. using entity framework , need store references users. don't want save ad users in database. 1 way store user guid string in entity.
class entity { string userguid ; }
is possible this:
class entity { userprincipal user; }
instead of passing string guid pass object , make entity framework treat association if userprincipal object entity. doesn't have class userprincipal, class. deal objects rather strings. querying active directory not problem.
in summary, able associate entity non-entity class storing string guid in database loading object.
[update]
many classes might have multiple associations ad users , can vary base class not solution. example, might have class this:
class message { public user sender; public user recipient; public list<user> mentionedusers; }
this not class using illustrates point. ideally user guid stored in message entity table loaded user entity framework other entites.
i thinking creating user wrapper entity class guid , retrieve properties static methods avoid this.
seems easy enough code first:
public class entity { public string userguid { get; set; } [notmapped] private userprincipal? _user; [notmapped] public userprincipal user { { if (!_user.hasvalue) _user = userprincipal.getuser(this.userguid); // make static easier re-use. return _user.value; } set { userguid = value.userguid; _user = value; } } }
[notmapped]
friend here (it's in system.componentmodel.dataannotations
). simplify things returning function call every time user get
, eg: get { return this.getuser(); }
, remove _user
field, impact performance.
i'm not sure if need [notmapped]
on field (in case _user
), try , without.
for list of users:
public class entity { public list<string> userguids { get; set; } [notmapped] private list<userprincipal> _users; [notmapped] public list<userprincipal> users { { if (_users != null) _users = userprincipal.getusers(this.userguids); return _users; } set { this.userguids = value.select(u => u.userguid).tolist(); _users = value; } } }
unfortunately there's not more elegant way implement ef. now, wouldn't difficult alter ef this. ef open source, fork , going if it's big enough project worth you.
Comments
Post a Comment