C# ASP.NET标识:在类库项目中为ASP.NET标识使用单个上下文
我希望将ASP.NET标识移植到我的业务逻辑中,而不是在API级别使用它。我创建了两个数据库上下文,即dbcontext和identitycontext来处理这个问题,因为我正在为标识表和其他实体使用一个数据库。问题是我想在BLL中使用一个上下文,但它不能正常工作。我还尝试创建dbcontext和identitydbcontext,但只有dbcontext将数据提交到数据库表中,而identitydbcontext未能将数据提交到单个数据库中的标识表中。有没有人能解决我如何使用entityframework反向工程生成的DAL模型在BLL类库项目中使用单个数据库上下文提交数据的问题 数据访问层 1.entityframework反向工程生成的上下文类C# ASP.NET标识:在类库项目中为ASP.NET标识使用单个上下文,c#,entity-framework,asp.net-identity,dbcontext,C#,Entity Framework,Asp.net Identity,Dbcontext,我希望将ASP.NET标识移植到我的业务逻辑中,而不是在API级别使用它。我创建了两个数据库上下文,即dbcontext和identitycontext来处理这个问题,因为我正在为标识表和其他实体使用一个数据库。问题是我想在BLL中使用一个上下文,但它不能正常工作。我还尝试创建dbcontext和identitydbcontext,但只有dbcontext将数据提交到数据库表中,而identitydbcontext未能将数据提交到单个数据库中的标识表中。有没有人能解决我如何使用entityfra
public partial class TwiksDbContext : IdentityDbContext
{
static TwiksDbContext()
{
Database.SetInitializer<TwiksDbContext>(null);
}
public TwiksDbContext()
: base("Name=TwiksDbContext")
{
}
public DbSet<AspNetRole> AspNetRoles { get; set; }
public DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
public DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
public DbSet<AspNetUser> AspNetUsers { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<BlogPost> BlogPosts { get; set; }
public DbSet<CommentEntityTracker> CommentEntityTrackers { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<ContactCategory> ContactCategories { get; set; }
public DbSet<ContactMessage> ContactMessages { get; set; }
public分部类TwiksDbContext:IdentityDbContext
{
静态TwiksDbContext()
{
Database.SetInitializer(null);
}
公共TwiksDbContext()
:base(“Name=TwiksDbContext”)
{
}
公共DbSet AspNetRoles{get;set;}
公共DbSet AspNetUserClaims{get;set;}
公共DbSet AspNetUserLogins{get;set;}
公共DbSet AspNetUsers{get;set;}
公共数据库集作者{get;set;}
公共DbSet BlogPosts{get;set;}
公共DbSet commentitytrackers{get;set;}
公共DbSet注释{get;set;}
公共DbSet ContactCategories{get;set;}
公共数据库集联系人消息{get;set;}
2) Repository类,其中将为整个项目创建单个上下文
public class EfRepository<T>:IRepository<T> where T:class
{
private readonly DbContext _context;
private readonly DbSet<T> _set;
public EfRepository()
{
_context = new TwiksDbContext();
_context.Configuration.LazyLoadingEnabled = false;
_set = _context.Set<T>();
}
public void AddEntity(T entity)
{
try
{
_set.Add(entity);
_context.SaveChanges();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
公共类eRepository:IRepository,其中T:class
{
私有只读DbContext\u context;
专用只读数据库集;
公共电子储蓄
{
_context=new TwiksDbContext();
_context.Configuration.LazyLoadingEnabled=false;
_set=_context.set();
}
公共无效附加值(T实体)
{
尝试
{
_设置、添加(实体);
_SaveChanges();
}
捕获(例外情况除外)
{
抛出新异常(例如消息);
}
}
业务逻辑层
1.在此项目中实现所有标识方法的自定义成员类
public class Membership
{
private readonly IdentityDbContext _context;
private UserManager<IdentityUser> userManager;
private RoleManager<IdentityRole> roleManager;
//private UserManagerFactory
public Membership()
{
_context = new TwiksDbContext();
// _context.Configuration.LazyLoadingEnabled = false;
var userStore = new UserStore<IdentityUser>(_context);
userManager = new UserManager<IdentityUser>(userStore);
var roleStore = new RoleStore<IdentityRole>(_context);
roleManager = new RoleManager<IdentityRole>(roleStore);
}
public DbInteractionNotification CreateRole(IdentityRole role)
{
try
{
if (roleManager.FindByNameAsync(role.Name) != null)
return DbInteractionNotification.EntryAlreadyExist;
else
return (roleManager.CreateAsync(role).IsCompleted) ? DbInteractionNotification.Success : DbInteractionNotification.FatalError;
}
catch (Exception)
{
return DbInteractionNotification.FatalError;
}
}
公共类成员资格
{
私有只读标识DybContext_上下文;
私有用户管理器用户管理器;
私人角色经理;
//私有用户管理器工厂
公众会员资格()
{
_context=new TwiksDbContext();
//_context.Configuration.LazyLoadingEnabled=false;
var userStore=新的userStore(_上下文);
userManager=新的userManager(userStore);
var roleStore=新的roleStore(_上下文);
roleManager=新roleManager(roleStore);
}
public DbInteractionNotification CreateRole(IdentityRole角色)
{
尝试
{
if(roleManager.findbynamesync(role.Name)!=null)
返回DbInteractionNotification.EntryAlreadyExist;
其他的
返回(roleManager.CreateAsync(role).IsCompleted)?DbInteractionNotification.Success:DbInteractionNotification.FatalError;
}
捕获(例外)
{
返回DbInteractionNotification.FatalError;
}
}
应用程序配置文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="TwiksDbContext" connectionString="Data Source=.;Initial Catalog=SchoolTDb;Integrated Security=True;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
尚不清楚什么“工作不正常”意味着当使用single ContextThank@raderick时,我想尝试使应用程序在单个数据库上下文上运行,标识实体应该将数据提交到我的数据库中。我想直接实现UserManager和RoleManager类中的方法。我想在我的BLL类库项目中实现这一点。