C# Asp.net C应用程序用户的多对多关系
诚然,我在深水区工作,尽管我一直在浏览许多“类似”的帖子,但我似乎无法理解这一点 我正试图与公司模型和应用程序用户建立联系。 该公司持有许多非盟,非盟可以持有许多公司。虽然当我创建一家公司并从我选中的selectList中选择不同的用户时,确实传递了AU-ID 警告:我只是一个新手,我很难弄清楚遵循指南和其他什么,所以可能会有明显的错误 模范公司:C# Asp.net C应用程序用户的多对多关系,c#,asp.net,entity-framework,relational-database,asp.net-identity,C#,Asp.net,Entity Framework,Relational Database,Asp.net Identity,诚然,我在深水区工作,尽管我一直在浏览许多“类似”的帖子,但我似乎无法理解这一点 我正试图与公司模型和应用程序用户建立联系。 该公司持有许多非盟,非盟可以持有许多公司。虽然当我创建一家公司并从我选中的selectList中选择不同的用户时,确实传递了AU-ID 警告:我只是一个新手,我很难弄清楚遵循指南和其他什么,所以可能会有明显的错误 模范公司: 通过正确配置的类,您应该能够执行类似的操作: var userList = new List<ApplicationUser>(); f
通过正确配置的类,您应该能够执行类似的操作:
var userList = new List<ApplicationUser>();
foreach (string i 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将自动进行联动。注意:您不需要使用UserManager来检索用户-它可以从context.users获得。您的公司模型具有公共列表UserId-这是字符串的集合。摆脱它,使用你的ApplicationUsers集合。请看这个类似的问题:谢谢你的评论。我已经更新了代码,但是收到一个验证错误。。我不确定这是否是你的意思。这是你建议的方法吗?`虽然链接是相似的,但我并没有试图创建一个新用户,只是通过一个关系表将两者链接起来。
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one 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; }
}
[HttpPost]
public async Task<ActionResult> CreateCompany(CreateCompanyViewModel model)
{
if (ModelState.IsValid)
{
DAL.CompanyContext context = new DAL.CompanyContext();
// Creating object from 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 and saving
context.Companies.Add(newCompany);
await context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(model);
}
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");
});
}
}
[HttpPost]
public async Task<ActionResult> CreateCompany(CreateCompanyViewModel model)
{
if (ModelState.IsValid)
{
DAL.CompanyContext context = new DAL.CompanyContext();
// Creating object from 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 i in model.SelectedApplicationUserId)
{
ApplicationUser user = new ApplicationUser();
user.Id = i;
newCompany.Users.Add(user);
}
//newCompany.Users = model.SelectedApplicationUserId;
// adding and saving
context.Companies.Add(newCompany);
await context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(model);
}
//newCompany.Users = model.SelectedApplicationUserId;
// adding and saving
context.Companies.Add(newCompany);
try
{
int response = await context.SaveChangesAsync();
}
catch(Exception exception)
{
}
foreach (string i 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);
var userList = new List<ApplicationUser>();
foreach (string i 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();