简体   繁体   English

在没有EF Core参考的ASP.NET项目中使用EF存储吗?

[英]Using EF stores in an ASP.NET project without EF Core reference?

The recommended way to use default ASP.NET Identity with EF Core involves putting the following in the ConfigureServices method of your ASP.NET application's Startup class: 将默认ASP.NET标识与EF Core结合使用的推荐方法是将以下内容放入ASP.NET应用程序的Startup类的ConfigureServices方法中:

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(
        Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();

This requires 2 things that don't seem like they belong in the ASP.NET Web project (a presentation-layer project): a reference to EF Core (to get the AddEntityFrameworkStores extension method), and a reference to ApplicationDbContext (which I'd have thought should be internal to the data access code in a persistence layer). 这需要AddEntityFrameworkStores似乎不属于ASP.NET Web项目(表示层项目)的东西:对EF Core的引用(以获取AddEntityFrameworkStores扩展方法),以及对ApplicationDbContext的引用(我d认为应该在持久层的数据访问代码内部)。

How can I avoid these references and separate concerns properly while still using this configuration for my site's identity? 在仍然使用此配置作为我的网站身份的同时,如何避免这些引用并适当地分离关注点?

It's worth noting that in .NET Core, references are transitive. 值得注意的是,在.NET Core中,引用是可传递的。 This means that if WebProj references BLLProj and BLLProj references DALProj : 这意味着,如果WebProj引用BLLProjBLLProj引用DALProj

WebProj -> BLLProj -> DALProj

then WebProj still gets all the references from DALProj . 然后WebProj仍然从DALProj获取所有引用。

Having said that, you can delegate the config of this to your data project in the same way the AddDbContext extension method works in your code. 话虽如此,您可以使用AddDbContext扩展方法在代码中起作用的相同方式,将此配置委派给数据项目。 This means the web project won't have any direct reference to any DAL objects. 这意味着该Web项目将没有任何直接引用到任何DAL对象。

For example, either in your BLL layer or even a completely separate project, you could have an extension method that will add your services to the DI container, something like this for example: 例如,在您的BLL层甚至是一个完全独立的项目中,您都可以使用扩展方法将服务添加到DI容器中,例如:

public static class ServiceCollectionExtensions
{
    public static void AddDataAndIdentity(this IServiceCollection services, IConfiguration config)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                config.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();
    }
}

And in your web project, call it in the ConfigureServices method: 并在您的Web项目中,在ConfigureServices方法中调用它:

services.AddDataAndIdentity(Configuration);

Additionally, there are tools that help you to visualise project dependencies. 此外,还有一些工具可以帮助您可视化项目依赖性。 If you have Visual Studio Enterprise edition, you can create a code map . 如果您拥有Visual Studio Enterprise版本,则可以创建一个代码映射 Other tools such as ReSharper can also help. 其他工具(例如ReSharper)也可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM