【发布时间】:2015-09-07 16:48:12
【问题描述】:
我在我的脑海中使用 POCO 类和持久性无知原则开发了我的数据层。我的代码是这样的
POCO 类
public class Report
{
public int Id { get; set; }
public string Name { get; set; }
...
}
}
仓库接口
public interface IReportRepository
{
IEnumerable<Report> GetAllReports();
Report GetReport(int reportId);
...
}
和接口实现先用Entity框架代码,比如
public class EFReportRepository : BaseRepository, IReportRepository
{
public EFReportRepository(string connectionString)
: base(connectionString)
{
}
public IEnumerable<Report> GetAllReports()
{
return DbContext.Reports.Include(r => r.AggregationParameters).Include(r => r.ErrorAttachment).Include(r => r.ExcelAttachment).Include(r => r.XmlAttachment).ToList();
}
public Report GetReport(int reportId)
{
return DbContext.Reports.Include(r => r.AggregationParameters).Include(r => r.ErrorAttachment).
Include(r => r.ExcelAttachment).Include(r => r.XmlAttachment).SingleOrDefault(r => r.Id == reportId);
}
在添加授权(asp.net 身份)之前一切正常。
using Microsoft.AspNet.Identity.EntityFramework;
namespace DataRepository.Models
{
public class MyUser : IdentityUser
{
}
}
现在每个报告都有创建此报告的用户
public class Report
{
public int Id { get; set; }
public string Name { get; set; }
...
public MyUser CreatedByUser { get; set; }
public string CreatedByUserId { get; set; }
...
}
}
由于从 IdentityUser 类继承,现在我与实体框架有紧密的依赖关系,我想省略它。
我怎样才能排除这种依赖?我会感谢任何帮助)
更新
现在问题在于创建 UserManager 对象。
UserManager 需要 IUserStore 作为输入参数。这个接口在Microsoft.AspNet.Identity.EntityFramework命名空间中有实现,叫做UserStore,所以我的代码是这样的
manager = new UserManager<MyUser>(new UserStore<MyUser>(MyDbContext)).
但是现在 MyUser 类不是从 IdentityUser 继承的,所以这段代码被破坏了(UserStore 需要这种类型的类)
【问题讨论】:
-
我找到了this。也许会对某人有所帮助
标签: c# entity-framework asp.net-identity poco