将控制器名称和操作名称存储到我的自定义表中,并将它们添加到 CodeFirst Asp.Net MVC 项目中的标识中



老年人,
我在 ASP.Net Web应用程序上使用 ASP.NET Identity,并将我的自定义表(及其关系)添加到我的CodeFirst ASP.Net MVC项目的Identity中。
自定义表格 :

  1. MVC控制器
  2. 行动Tbls
  3. 组Tbls
  4. AspNetUser_Action
  5. AspNetUser_Group
  6. Action_Group

    • 这是我的数据库映像的示意图: 点击这里

为了创建自定义表,我在 IdentityModels.cs 中添加了一些代码。
身份模型.cs :

namespace Admin_Base_SN.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
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
return userIdentity;
}
public virtual AspNetUser_Action AspNetUser_Action { get; set; }
public virtual AspNetUser_Group AspNetUser_Group { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

public DbSet<MvcController> MvcController { get; set; }
public DbSet<ActionsTbls> ActionsTbls { get; set; }
public DbSet<AspNetUser_Action> AspNetUser_Actions { get; set; }
public DbSet<GroupsTbl> GroupsTbls { get; set; }
public DbSet<Action_Group> Action_Groups { get; set; }
public DbSet<AspNetUser_Group> AspNetUser_Groups { get; set; }
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// one-to-zero or one relationship between ApplicationUser and Customer
// UserId column in Customers table will be foreign key
modelBuilder.Entity<ApplicationUser>()
.HasOptional(m => m.AspNetUser_Action)
.WithRequired(m => m.ApplicationUser)
.Map(p => p.MapKey("AspNetUser_Id"));
modelBuilder.Entity<ApplicationUser>()
.HasOptional(m => m.AspNetUser_Group)
.WithRequired(m => m.ApplicationUser)
.Map(p => p.MapKey("AspNetUser_Id"));
}
}
}

我想要
什么首先,我想将项目的所有控制器名称和动作名称保存到单独的列表中,然后将它们插入Mvc控制器表和ActionsTbl表中。我的意思是创建数据库时,列表会自动对其表惰性。

  • 我认为最好在标识中添加一个新的自定义函数,以便将列表插入表中。

我感谢您为解决我的问题所做的努力。

如果所需的类型是控制器操作结果(没有 Web api),则 您可以使用反射获取控制器和操作

var controllers = Assembly.GetAssembly(typeof(*any type in assembly*))
.GetTypes()
.Where(x => x.IsSubclassOf(typeof(Controller)))
.ToList();
var result = controllers
.Select(type => new
{
ControllerType = type,
Actions = type.GetMethods()
.Where(m => m.ReturnType == typeof(ActionResult) || m.ReturnType.IsSubclassOf(typeof(ActionResult))).ToList()
})
.ToList();

由于使用的是 EF,因此可以将所有必要的逻辑放入种子方法 并启用自动迁移

最新更新