entity framework many to many still execting foreign key after fluent api -
i've created entity framework m:m relationship between user identity class , class called group.
although i've given both classes icollection property linking other one, didn't automatically create many many table , i've instead created fluent api - groupusers table. appears ok.
when try register user, fails. looking @ sql generated appears expecting group_id field present in users table - not sure why it's looking in separate table.
the group class (entity has id property)
public class group : entity { public group() { this.users = new hashset<user>(); } [maxlength(100)] public string name { get; set; } public virtual icollection<user> users { get; set; } } the user class (customised application user class)
public class user : identityuser<int, customuserlogin, customuserrole, customuserclaim> { public user() { this.groups = new hashset<group>(); } [required] [maxlength(100)] public string firstname { get; set; } [required] [maxlength(100)] public string lastname { get; set; } public virtual icollection<group> groups { get; set; } public async task<claimsidentity> generateuseridentityasync(usermanager<user, int> manager, string authenticationtype) { // note authenticationtype must match 1 defined in cookieauthenticationoptions.authenticationtype var useridentity = await manager.createidentityasync(this, authenticationtype); // add custom user claims here return useridentity; } } as can see above both have property relationship other class.
fluent api:
modelbuilder.entity<user>().hasmany(u => u.groups).withmany(g => g.users).map(x => { x.mapleftkey("groupid"); x.maprightkey("userid"); x.totable("groupusers"); }); all above creates tables: user, group, , groupusers.
i have expected working is.
unfortunately, when try register user still looks expecting group_id present in user table:
exec sp_executesql n'select [limit1].[id] [id], [limit1].[firstname] [firstname], [limit1].[lastname] [lastname], [limit1].[email] [email], [limit1].[emailconfirmed] [emailconfirmed], [limit1].[passwordhash] [passwordhash], [limit1].[securitystamp] [securitystamp], [limit1].[phonenumber] [phonenumber], [limit1].[phonenumberconfirmed] [phonenumberconfirmed], [limit1].[twofactorenabled] [twofactorenabled], [limit1].[lockoutenddateutc] [lockoutenddateutc], [limit1].[lockoutenabled] [lockoutenabled], [limit1].[accessfailedcount] [accessfailedcount], [limit1].[username] [username], [limit1].[group_id] [group_id] ---why here?? ( select top (1) [extent1].[id] [id], [extent1].[firstname] [firstname], [extent1].[lastname] [lastname], [extent1].[email] [email], [extent1].[emailconfirmed] [emailconfirmed], [extent1].[passwordhash] [passwordhash], [extent1].[securitystamp] [securitystamp], [extent1].[phonenumber] [phonenumber], [extent1].[phonenumberconfirmed] [phonenumberconfirmed], [extent1].[twofactorenabled] [twofactorenabled], [extent1].[lockoutenddateutc] [lockoutenddateutc], [extent1].[lockoutenabled] [lockoutenabled], [extent1].[accessfailedcount] [accessfailedcount], [extent1].[username] [username], [extent1].[group_id] [group_id] [dbo].[aspnetusers] [extent1] ((upper([extent1].[username])) = (upper(@p__linq__0))) or ((upper([extent1].[username]) null) , (upper(@p__linq__0) null)) ) [limit1]',n'@p__linq__0 nvarchar(4000)',@p__linq__0=n'myemail@myemail.com' when run sql seperately, :
invalid column name 'group_id'.
any ideas why it's not working correctly , why group_id expected there, though it's specified in fluent api shouldn't be?
thanks,
Comments
Post a Comment