我需要你帮助指导可能在MVC4中实现模型的好方法。 我会让你看看我的模特。但我真的不知道如何将其链接到MVC4中的Membership Provider 我想构建招标申请系统,我有以下模型
要求:每个项目都有几个要求。(完成)
2.我和很多人的关系是否正确?在项目和需求表之间。?
现在这些是我的上下文模型:
public class ProjectContext : DbContext
{
public ProjectContext()
: base("ProjectsDB")
{
}
public DbSet<ProjectEntry> Entries { get; set; }
public DbSet<Requiernments> RequiernmentEntries { get; set; }
//public DbSet<UserProfile> UserProfiles { get; set; }
}
public class ProjectEntry
{
[Key]
public int ID { get; set; }
[Required]
public string ProjectName { get; set; }
public string Description { get; set; }
public string Statue {get; set; }
public string UplodedFiles { get; set; }
public string Budget { get; set; }
public string EstimateTime { get; set; }
public string Criterias { get; set; }
public DateTime? DueDate { get; set; }
}
public class Requiernments
{
[Key]
public int RequiernmentId { get; set; }
public int ID { get; set; }
public string RequiernmentName { get; set; }
/// <summary>
/// 1: Must to Have
/// 2: Nice to Have
/// 3: Should have
/// </summary>
public string RequiernmentType { get; set; }
public string RequiernmentPrioritet { get; set; }
public float RequiernmenWhight { get; set; }
public string ProviderAnswer { get; set; }
public string ProviderComments{ get; set; }
}:
更新2:
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
if (!Roles.RoleExists("Admin"))
Roles.CreateRole("Admin");
if (!Roles.RoleExists("Member"))
Roles.CreateRole("Member");
if (!Roles.RoleExists("Tender"))
Roles.CreateRole("Tender");
if (!Roles.RoleExists("Provider"))
Roles.CreateRole("Provider");
WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
new
{
EmailAddress = model.EmailAddress
}, false);
Roles.AddUserToRole(model.UserName, "Member");
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
和
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=user-Pc\SQL2012;Initial Catalog=MemberDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MemberDB.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
图片:
http://i58.tinypic.com/2rp8i86.png
答案 0 :(得分:0)
如果我理解正确,在您的申请中,您有两个角色,招标和供应商。此外,您希望投标能够添加项目,然后将需求与项目相关联。
为了达到这个目的,首先你需要配置 SimpleMembershipProvider 来扮演两个角色&#34; 招标&#34;和&#34; 供应商&#34;
首先在配置文件中,使用 SimpleMembershipProvider 替换经典成员资格提供程序
启用迁移
种子成员资格和角色
protected override void Seed(MovieDb context)
{
//context.Movies.AddOrUpdate(...);
// ...
SeedMembership();
}
private void SeedMembership()
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection",
"UserProfile", "UserId", "UserName", autoCreateTables: true);
var roles = (SimpleRoleProvider) Roles.Provider;
var membership = (SimpleMembershipProvider) Membership.Provider;
if (!roles.RoleExists("Admin"))
{
roles.CreateRole("Admin");
}
if (membership.GetUser("sallen",false) == null)
{
membership.CreateUserAndAccount("sallen", "imalittleteapot");
}
if (!roles.GetRolesForUser("sallen").Contains("Admin"))
{
roles.AddUsersToRoles(new[] {"sallen"}, new[] {"admin"});
}
}
步骤1,2,3参考: Scott Allen's blog
现在创建模型
public class Tender
{
public int TenderId { get;set;}
public int UserId {get;set;} //this links to the userid in the UserProfiles table
public virtual ICollection<Project> Projects {get;set;}
}
public class Project
{
public int ProjectId {get;set;}
public int TenderId {get;set;}
public virtual Tender Tender {get;set;}
public virtual ICollection<Supplier> Suppliers {get;set;}
public virtual ICollection<Requirement> Requirements {get;set;}
}
public class Supplier
{
public int SupplierId {get;set;}
public virtual ICollection<Project> Projects {get;set;}
}
public class Requirement
{
public int RequirmentId {get;set;}
public int ProjectId {get;set;}
public virtual Project Project {get;set;}
}
因为供应商可以出价多个项目,而项目可以有多个出价者,因此供应商和项目有多对多关系,您可能希望有一个映射表。
在OnModelCreating方法中,
modelBuilder.Entity<Project>()
.HasMany(p => p.Suppliers)
.WithMany(s => s.Projects)
.Map(map =>
{
map.ToTable("Project_Supplier_Map")
.MapLeftKey("SupplierId")
.MapRightKey("ProjectId");
});
现在你有了你的模型,只需要使用Authorize属性
来装饰你的类