简体   繁体   English

使用继承的Context时如何消除Microsoft.AspNet.Identity.EntityFramework依赖关系?

[英]How to eliminate the Microsoft.AspNet.Identity.EntityFramework dependency when using an inherited Context?

I'm creating a base C# library for ASP.NET MVC 5 projects for the company I work for, so when we have to share source code, we would include references to Enterprise.Classes library and the original dependencies are not required since this class would integrate all functionality. 我正在为我工​​作的公司创建ASP.NET MVC 5项目的基础C#库,因此当我们必须共享源代码时,我们将包括对Enterprise.Classes库的引用,并且因为此类而不需要原始依赖项将集成所有功能。

For the DbContext , we have created a custom context that inherits from IdentityDbContext , but whenever it is inherited to create new contexts, the Microsoft.AspNet.Identity.EntityFramework Nuget package is required to be installed to work. 对于DbContext ,我们创建了一个继承自IdentityDbContext的自定义上下文,但是每当继承它以创建新的上下文时,都需要安装Microsoft.AspNet.Identity.EntityFramework Nuget包才能正常工作。

So far this is the code for the custom DbContext : 到目前为止,这是自定义DbContext的代码:

[DbConfigurationType(typeof(EnterpriseDbConfiguration))]
public class EnterpriseDbContext : IdentityDbContext<EnterpriseUser, EnterpriseRole, long, EnterpriseUserLogin,
    EnterpriseUserRole, EnterpriseUserClaim>
{
    public EnterpriseDbContext(string contextName = "DefaultEnterpriseConnection") : base(contextName)
    {
        /* Entities, Configurations and stuff */
    }
}

And here is an example of the EnterpriseUser class (which would be the IdentityUser class): 这是EnterpriseUser类的示例(将是IdentityUser类):

public class EnterpriseUser : IdentityUser<long, EnterpriseUserLogin, EnterpriseUserRole, EnterpriseUserClaim>
{
    public EnterpriseUser()
    {
        RegistrationDate = DateTime.Now;
        Enabled = true;
    }

    /* Additional Properties */
}

Here is an example of an inheriting context, when EnterpriseDbContext is marked for inheritance it requires the Microsoft.AspNet.Identity.EntityFramework Nuget package to be installed: 这是一个继承上下文的示例,当EnterpriseDbContext被标记为继承时,它需要安装Microsoft.AspNet.Identity.EntityFramework Nuget包:

public class InheritedEnterpriseContext : EnterpriseInfrastructure.Data.EnterpriseDbContext
{

}

How can I build the EnterpriseDbContext so when it is inherited it won't require the Nuget package to be installed? 我该如何构建EnterpriseDbContext以便在继承它时不需要安装Nuget包?

Captura Visual Studio

The issue will be that by exposing a class that directly inherits, consumers of that class will need to know about all public shared members, requiring a dependency on the AspNet.Identity.EntityFramework library whether you intend to use them or not. 问题在于,通过公开直接继承的类,该类的使用者将需要了解所有公共共享成员,这需要依赖于AspNet.Identity.EntityFramework库,无论您是否打算使用它们。

Edit: Corrections around IdentityDbContext and it's purpose... 编辑:围绕IdentityDbContext的更正及其目的...

IdentityDbContext is used for ASP.Net's authentication/authorization so your options are to either extend IdentityDbContext in which case you will need to reference ASPNet.Identity.EntityFramework anywhere your DbContext is referenced, or use separate contexts for your Identity and Application information. IdentityDbContext用于ASP.Net的身份验证/授权,因此您的选择是扩展IdentityDbContext,在这种情况下,您将需要在引用DbContext的任何地方引用ASPNet.Identity.EntityFramework,或者对标识和应用程序信息使用单独的上下文。

You can define your EnterpriseUser/Role classes and construct an IdentityDbContext Instance to provide to your UserStore etc. for Auth. 您可以定义您的EnterpriseUser / Role类,并构造一个IdentityDbContext实例以提供给UserStore等进行Auth。 Then define your EnterpriseDbContext to extend DbContext. 然后定义您的EnterpriseDbContext以扩展DbContext。 It too can define DbSets for EnterpriseUser/Role as you see fit. 您也可以根据需要为EnterpriseUser / Role定义DbSet。 This DbContext can be shared/referenced by other code. 此DbContext可以被其他代码共享/引用。 Any MVC project that you want to use the IdentityDbContext will obviously need ASPNet.Identity.EntityFramework. 您要使用IdentityDbContext的任何MVC项目显然都需要ASPNet.Identity.EntityFramework。 Other libraries would use your EnterpriseDbContext /w just the normal EF reference. 其他库将仅使用常规EF参考来使用EnterpriseDbContext / w。

public class EnterpriseDbContext : DbContext
{
    public DbSet<EnterpriseUser> EnterpriseUsers { get; set; }
    // .. Your data entities.
}

If EnterpriseUser extends IdentityUser you will likely still have a dependency to .Identity.EntityFramework. 如果EnterpriseUser扩展了IdentityUser,则您可能仍将依赖于.Identity.EntityFramework。 In this case you should define an alternative "User" entity for your EnterpriseDbContext that proxies for EnterpriseUser and use that in it's place for the application DbContext. 在这种情况下,您应该为EnterpriseDbContext定义一个替代的“ User”实体,该实体替代EnterpriseUser并将其用于应用程序DbContext。

Ie

// Can't use this in our EnterpriseDbContext because IdentityUser will require ref. to .Identity.EntityFramework
public class EnterpriseUser : IdentityUser<Guid>
{
   // Stuff.
}

// Use this instead in our EnterpriseDbContext to map to the same table
[Table("EnterpriseUsers")]
public class User
{
   public Guid Id { get; set; }
   public string UserName { get; set; }
   // Stuff...
}

暂无
暂无

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

相关问题 vsts构建在Microsoft.AspNet.Identity.EntityFramework上失败 - vsts build fails on Microsoft.AspNet.Identity.EntityFramework 不使用Microsoft.AspNet.Identity.EntityFramework的自定义Asp.net MVC 5身份验证 - Custom Asp.net mvc 5 authentication without using Microsoft.AspNet.Identity.EntityFramework .NET Standard 2.0中的Microsoft.AspNet.Identity和Microsoft.AspNet.Identity.EntityFramework - Microsoft.AspNet.Identity and Microsoft.AspNet.Identity.EntityFramework in .NET Standard 2.0 Nuget错误:“ EntityFramework 5.0.0”与“ Microsoft.AspNet.Identity.EntityFramework 1.0.0不兼容” - Nuget error: 'EntityFramework 5.0.0' is not compatible with 'Microsoft.AspNet.Identity.EntityFramework 1.0.0 为什么IdentityUser类在Microsoft.AspNet.Identity.EntityFramework命名空间中而不在Core包中? - Why is the IdentityUser class in the Microsoft.AspNet.Identity.EntityFramework namespace and not in the Core package? 自定义Microsoft.AspNet.Identity.EntityFramework.UserStore <TUser> - custom Microsoft.AspNet.Identity.EntityFramework.UserStore<TUser> 无法从“字符串”转换为“ Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole - cannot convert from 'string' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole &#39;Microsoft.AspNet.Identity.EntityFramework.IdentityUser&#39;未标记为可序列化 - 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser' is not marked as serializable Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的外键? - Foreign Key To Microsoft.AspNet.Identity.EntityFramework.IdentityUser? 使用openiddict时如何避免使用Microsoft的Microsoft.AspNet.Identity包 - How do I avoid using Microsofts's Microsoft.AspNet.Identity package when using openiddict
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM