Asp.net C# Many-to-Many Relations EF ApplicationUser -


i admittedly on deep water , although have been looking looking through numerous 'similar' posts, cant seem figure 1 out.

i trying establish connection "company model" , applicationuser. company holds many au , au's can hold many companies. although when create company , select different users selectlist (which checked, pass au-id.

warning: i'm rookie , had troubles figuring out following guides , not - there might obvious mistakes.

model company:

 public company()     {         this.users = new list<applicationuser>();     }     public int id { get; set; }     public string name { get; set; }     public string address { get; set; }     public string zip { get; set; }     public string city { get; set; }     public string cvr { get; set; }     public string telephone { get; set; }     public string email { get; set; }     public string website { get; set; }     public string billingemail { get; set; }     [foreignkey("userid")]     public list<string> userid { get; set; }     public virtual list<applicationuser> users { get; set; } } 

model applicationuser

  public class applicationuser : identityuser {     public async task<claimsidentity> generateuseridentityasync(usermanager<applicationuser> manager)     {         // note authenticationtype must match 1 defined in cookieauthenticationoptions.authenticationtype         var useridentity = await manager.createidentityasync(this, defaultauthenticationtypes.applicationcookie);         // add custom user claims here         useridentity.addclaim(new claim("firstname", this.firstname.tostring()));         useridentity.addclaim(new claim("lastname", this.lastname.tostring()));         return useridentity;     }     public applicationuser()     {         this.companies = new list<company>();     }      public string firstname { get; set; }     public string lastname { get; set; }     public virtual list<company> companies { get; set; } } 

controller

 [httppost]     public async task<actionresult> createcompany(createcompanyviewmodel model)     {          if (modelstate.isvalid)         {               dal.companycontext context = new dal.companycontext();              // creating object recieved form             domainmodels.company newcompany = new domainmodels.company();               newcompany.address = model.address;             newcompany.billingemail = model.billingemail;             newcompany.city = model.city;             newcompany.cvr = model.cvr;             newcompany.email = model.email;             newcompany.name = model.name;             newcompany.telephone = model.telephone;             newcompany.website = model.website;             newcompany.zip = model.zip;             newcompany.userid = model.selectedapplicationuserid;             // adding , saving             context.companies.add(newcompany);             await context.savechangesasync();              return redirecttoaction("index");         }         return view(model);      } 

dbcontext

 public dbset<company> companies { get; set; }     protected override void onmodelcreating(dbmodelbuilder mb)      {         base.onmodelcreating(mb);         mb.entity<company>()              .hasmany(c => c.users)             .withmany(d =>d.companies)             .map(m=>             {             m.mapleftkey("company_id");             m.maprightkey("applicationuser_id");             m.totable("companyapplicationusers");             });     }      } 

the context confused about. searching google, find lot of posts indicating need create onmodelcreate, although thought ef automatically - mind you, has created table automatically, containing "companyapplicationusers" table (which why use informations).

the company created each time, nothing posted relational table.

i got feeling i'm messing around here - hope i'll learn! :)

thanks help.

update 1:

i changed model , instead point applicationuser (users) foreign key. loop through list of id's i'm receiving view , create user , add these newcompany list..

however while i'm unsure correct way, i'm receiving validationerror "validation failed 1 or more entities" on context.savechanges

the errors here related user create fill in list - require additional information such username. note, want relation, not creating actual user.

       [httppost]     public async task<actionresult> createcompany(createcompanyviewmodel model)     {          if (modelstate.isvalid)         {               dal.companycontext context = new dal.companycontext();              // creating object recieved form             domainmodels.company newcompany = new domainmodels.company();               newcompany.address = model.address;             newcompany.billingemail = model.billingemail;             newcompany.city = model.city;             newcompany.cvr = model.cvr;             newcompany.email = model.email;             newcompany.name = model.name;             newcompany.telephone = model.telephone;             newcompany.website = model.website;             newcompany.zip = model.zip;             foreach (string in model.selectedapplicationuserid)             {                 applicationuser user = new applicationuser();                 user.id = i;                 newcompany.users.add(user);             }             //newcompany.users = model.selectedapplicationuserid;             // adding , saving             context.companies.add(newcompany);             await context.savechangesasync();              return redirecttoaction("index");         }         return view(model);      } 

update 2

for ill make due 1 many relationship, can make work. in case, update applicationuser companyid each associated user. i'd still know how create many many, perhaps of im doing here, needed there aswell. cant figure out.

    //newcompany.users = model.selectedapplicationuserid;             // adding , saving              context.companies.add(newcompany);             try             {                int response = await context.savechangesasync();             }             catch(exception exception)             {              }              foreach (string in model.selectedapplicationuserid)             {                 var user = await usermanager.findbyidasync(i);                 user.companyid = newcompany.id;                 identityresult result = await usermanager.updateasync(user);             }             try             {              }             catch (exception exception)             {                 throw;             }              return redirecttoaction("index");         }         return view(model); 

you should able similar configured classes:

var userlist = new list<applicationuser>(); foreach (string in model.selectedapplicationuserids) {     userlist.add(new applicationuser {id = i}); }  var newcompany = new company {     address = model.address;     billingemail = model.billingemail;     city = model.city;     // etc.      users = userlist };  int response = await context.savechangesasync(); 

ef make linkages automatically. note don't need use usermanager retrieve users -- it's available context.users.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -