MVC 6 - 如何为Identity 2和Entity Framework 6

时间:2017-02-16 13:22:56

标签: asp.net-mvc entity-framework database-connection asp.net-identity

我已经在这上面旋转了一段时间,现在还没有找到任何关于这个主题的实质内容。

我已经实现了一个使用Identity Framework 2和Entity Framework 6的MVC 6项目。该应用程序可以正常使用本地数据库作为我的身份库。在第一个连接上,实体框架根据我的标识模型类创建标识表。我是根据这个book的项目构建的。关于使用Identity和我发现的所有在线样本的章节都没有涉及将实体框架指向远程数据库。

以下是我尝试过的连接字符串......

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

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

失败的方法1.我基于其他项目中的其他连接字符串。我删除了元数据参数,因为当我理解时,这指向edmx文件,但我没有一个,因为我采用代码优先方法并让身份和实体框架构建数据库。我想知道我是否在这里遗漏了一些东西,因为我一直采用数据库优先方法,直到现在。

<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文件,认为这必须存储所有元数据,并且实体框架可能会更新它并在应用程序连接到数据库后拥有所需的资源第一次。

<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.

最后失败的方法。我找到了许多在线资源,人们只是将元数据参数更改为&#34; res:// *&#34;。我猜这是一种通配符,可以找到元数据资源,如果它们存在...

<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

我的数据库上下文类

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很新,目前正在构建这个模块,以便我可以将它用于我今年将要开发的一些企业应用程序。

2 个答案:

答案 0 :(得分:1)

您需要指定providerName =&#34; System.Data.SqlClient&#34;而不是providerName =&#34; System.Data.EntityClient&#34;

例如

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

答案 1 :(得分:0)

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

$value = $array[0][0]["value"];

该工具为我生成了这个(我不需要对连接字符串做任何自定义操作)所以也许你可以删除并让它重新添加它? (我认为它确实如此)?

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