MVC 6-如何为身份2和实体框架6配置远程数据库连接6



我已经在此上旋转了一段时间,但无法在主题上找到任何实质性的东西。

我已经实现了一个使用Identity Framework 2和Entity Framework的MVC 6项目6.该应用程序可在本地数据库用作我的身份存储库中正常运行。在第一个连接上,实体框架根据我的身份模型类创建身份表。我是根据本书的项目建立的。有关使用身份和所有在线样本的章节,我发现没有将实体框架指向远程数据库。

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

本地数据库连接字符串。项目工作正常。

<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.我构建了一个空的身份。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.

最后失败的方法。我找到了很多在线资源,人们只是将元数据参数更改为" 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

我的数据库上下文类

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

您需要指定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"/>

这是我使用的连接字符串,并且无问题。

<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" />

工具为我生成了此工具(我不需要对连接字符串进行自定义的任何操作),所以也许您可以删除并让它重新添加它?(我认为这样做)?

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

最新更新