我希望将ASP.NET标识移植到我的业务逻辑中,而不是在API级别上使用它。我创建了两个数据库上下文,它们是dbcontext和identitycontext来处理这个问题,因为我使用单个数据库来识别身份表和其他实体。问题是我想在我的BLL中使用单个上下文但它不能正常工作。我还尝试创建dbcontext和identitydbcontext,但只有dbcontext将数据提交到数据库表中,而identitydbcontext无法将数据提交到我的单个数据库中的标识表中。有没有人有解决方案如何在我的BLL类库项目中使用单个数据库上下文提交数据,并使用entityframework逆向工程生成DAL中的模型?
数据访问层 1.实体框架逆向工程生成的上下文类
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; }
2)存储库类,其中将创建整个项目的单个上下文
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);
}
}
商业逻辑层 1.自定义成员资格类,其中所有Identity方法都在此项目中实现。
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;
}
}
应用程序配置文件
<?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>