MVC 6 - 如何为 Identity 2 和 Entity Framework 6 配置远程数据库连接

MVC 6 - How to configure remote database connection for Identity 2 and Entity Framework 6

我一直在研究这个问题有一段时间了,一直没能找到关于这个主题的任何实质性内容。

我已经实施了一个 MVC 6 项目,该项目利用身份框架 2 和 Entity Framework6。该应用程序在本地数据库用作我的身份存储库时运行良好。在第一次连接时,Entity Framework 根据我的身份模型 classes 创建身份表。我基于这个 book 的一个项目构建了它。关于使用 Identity 的章节和我找到的所有在线示例都没有涵盖将 entity framework 指向远程数据库。

这是我试验过的连接字符串...

本地数据库连接字符串。项目运行良好。

<add name="IdentityDb" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\IdentityDb.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

失败的方法 1. 我基于其他项目的其他连接字符串。我删除了元数据参数,因为根据我的理解,这指向 edmx 文件,但我没有,因为我采用代码优先方法并让身份和 entity frameworks 构建数据库.我想知道我是否遗漏了什么,因为直到现在我一直采用数据库优先方法。

<add name="IdentityDb" connectionString="provider=System.Data.SqlClient;provider connection string=&quot;data source=****;initial catalog=IdentityTest;Uid=*****;Pwd=*****;integrated security=False;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

返回异常详细信息。缺少元数据关键字。

System.ArgumentException: Some required information is missing from the connection string. The 'metadata' keyword is always required.

失败的方法 2。我构建了一个空的 Identity.edmx 文件,认为这一定是存储所有元数据的地方,也许 entity framework 会更新它并在应用程序连接后获得所需的资源第一次到数据库。

<add name="IdentityDb" connectionString="metadata=res://*/Identity.csdl|res://*/Identity.ssdl|res://*/Identity.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=****;initial catalog=IdentityTest;Uid=****;Pwd=****;integrated security=False;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

返回异常详细信息。缺少元数据。

System.Data.Entity.Core.MetadataException: Unable to load the specified metadata resource.

上次失败的方法。我发现很多在线资源,人们只是将元数据参数更改为 "res://*"。我猜这是某种通配符,可以定位元数据资源(如果存在的话)...

<add name="IdentityDb" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string=&quot;data source=****;initial catalog=IdentityTest;Uid=****;Pwd=****;integrated security=False;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

返回的异常详细信息(项目中有一个 identity.edmx 文件)

System.NotSupportedException: Model compatibility cannot be checked because the DbContext instance was not created using Code First patterns. DbContext instances created from an ObjectContext or using an EDMX file cannot be checked for compatibility.

返回的异常详细信息(项目中没有 identity.edmx 文件)

System.ArgumentException: Argument 'xmlReader' is not valid. A minimum of one .ssdl artifact must be supplied

我的数据库上下文class

public class AppIdentityDbContext : IdentityDbContext<AppUser>
{
    public AppIdentityDbContext() : base("name=IdentityDb") { }

    static AppIdentityDbContext()
    {
        // Seeds the database when the schema is first created through the Entity Framework.
        Database.SetInitializer<AppIdentityDbContext>(new IdentityDbInit());
    }

    // OWIN uses this method to create instances of this class when needed.
    public static AppIdentityDbContext Create()
    {
        return new AppIdentityDbContext();
    }

    public System.Data.Entity.DbSet<UAM.Models.Identity.AppRole> IdentityRoles { get; set; }
}

// Seed class for initially populating the identity database.
public class IdentityDbInit : DropCreateDatabaseIfModelChanges<AppIdentityDbContext>
{
    protected override void Seed(AppIdentityDbContext context)
    {
        PerformInitialSetup(context);
        base.Seed(context);
    }

    public void PerformInitialSetup(AppIdentityDbContext context)
    {
        // Initial database configuration
    }
}

任何帮助或见解将不胜感激。这样做的正确方法是什么?当我从使用本地数据库过渡到远程数据库时,我需要做些什么吗?让我知道是否应该再提供代码示例。我对 MVC 还很陌生,目前正在构建这个模块,以便我可以将它用于我今年要开发的一些企业应用程序。

这是我使用的连接字符串,它没有问题。

<add name="InventoryEntities" 
   connectionString="metadata=res://*/Entities.csdl|res://*/Entities.ssdl|res://*/Entities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sqlinstance;initial catalog=DBName;persist security info=True;user id=UID;password=PWD;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
   providerName="System.Data.EntityClient" />

该工具为我生成了这个(我不需要对连接字符串做任何自定义)所以也许你可以删除并让 re-add 它? (我认为它可以)?

此外,您根本没有重命名 EF 文件,是吗?如果这样做,则必须更新连接字符串以匹配。

您需要指定 providerName="System.Data.SqlClient" 而不是 providerName="System.Data.EntityClient"

例如

<add name="IdentityDb" connectionString="Data Source=*******;Database=IdentityTest;Integrated Security=false;User ID=**;Password=******;" providerName="System.Data.SqlClient"/>