简体   繁体   English

在使用身份v2和实体框架的Asp.net项目中,如何将所有代码优先模型和用户模型身份移至新项目

[英]In an Asp.net project using identity v2 and Entity Framework how can I move all my code first models and User model identity to a new project

I have a web API 2 project and I use Entity Framework code-first models which has some foreign keys to identity version 2 users table that is bound to webapi 2 project. 我有一个Web API 2项目,我使用Entity Framework代码优先模型,该模型具有一些外键,用于标识绑定到webapi 2项目的版本2用户表。 Since I have another provider project I have a reference to provider in my service (web api 2) project and since provider needs a reference to models putting models in service project cause circular reference. 由于我有另一个提供者项目,因此我在服务(Web api 2)项目中引用了提供者,并且由于提供者需要引用将模型放入服务项目中的模型,因此会引起循环引用。

Long story short I need to be able to move the code first models to a new project called Models to be used in both project. 长话短说,我需要能够将代码优先模型移到一个名为Models的新项目中,以在两个项目中使用。 The problem is that no matter how hard I tried I couldn't move the IdentityUser model to a separate project since it needs reference to Microsoft.AspNet.Identity.EntityFramework 问题是,无论我多么努力,我都无法将IdentityUser模型移至单独的项目,因为它需要引用Microsoft.AspNet.Identity.EntityFramework

Below is an example of one of the models I use and ApplicationUser derives from IdentityUser : 以下是我使用的一种模型的示例,我从IdentityUser派生了ApplicationUser

public class Application
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Owner { get; set; }
    public string Company { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PaymentInfo { get; set; }
    public DateTime CreateDate { get; set; }
    public DateTime ExpireDate { get; set; }

    public virtual ApplicationUser User { get; set; }
    public virtual ICollection<Billing> Billings { get; set; }
}

I had the same issue few months ago. 几个月前我遇到了同样的问题。

ApplicationUser has to be inherited from IdentityUser. ApplicationUser必须从IdentityUser继承。 And there is no other way to keep all models of your project in one class library project without using required references when you using Identity framework. 使用Identity框架时,没有其他方法可以将项目的所有模型保留在一个类库项目中,而无需使用必需的引用。

My suggestion is to use separate project (class library) to handle all the identity stuff rather than keeping it on your web API 2 project. 我的建议是使用单独的项目(类库)来处理所有身份信息,而不是将其保留在Web API 2项目中。 Then you can reference that project anywhere when you needed. 然后,您可以在需要时在任何地方引用该项目。 This will avoid having Web Api 2 project referencing on Models project. 这样可以避免在Models项目上引用Web Api 2项目。

Hope this helps. 希望这可以帮助。

暂无
暂无

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

相关问题 如何使用Asp.Net.Identity和Entity Framework 6在Asp.Net Core 2.0 Target Framework 4.6.1中设置身份? - How can I setup identity in Asp.Net Core 2.0 Target Framework 4.6.1 using Asp.Net.Identity and Entity Framework 6? 将SignalR Hub与ASP.NET MVC 5项目一起使用时,如何访问用户在Identity 2中的声明? - How can I access the user's Claims in Identity 2 when using SignalR Hub with an ASP.NET MVC 5 project? 如何在ASP.NET Identity框架中对用户进行身份验证? - How do I authenticate user in ASP.NET Identity framework? 如何使用ASP.NET Identity 2创建第一个root用户? - How to create the first root user using ASP.NET Identity 2? 如何在 asp.net mvc 5 中使用身份框架和代码优先方法自动为角色和用户创建表 - How to create tables for roles and users automatically using identity framework with code first approach in asp.net mvc 5 当我尝试使用 ASP.NET 核心标识创建新用户时,为什么我的 ApiKey 变量位于 null 中? - Why can it be that my ApiKey variable is in null when I try to create a new user using ASP.NET Core Identity? 身份模型中的实体框架ASP.NET MVC6,创建新记录,而不是将其链接到现有记录 - Entity Framework ASP.NET MVC6 in Identity Model, Creating a new record instead of linking it to existing record ASp.net实体框架多个身份用户实体 - ASp.net Entity Framework multiple identity user entities 如何将ASP.NET MVC Web应用程序的标识/身份验证部分移到类库中? - How can I move the identity/authentication part of my ASP.NET MVC web app into a class library? 实体框架和ASP.NET标识 - Entity framework and ASP.NET Identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM