【发布时间】:2017-02-16 13:22:55
【问题描述】:
我已经在这方面旋转了一段时间,但找不到任何关于该主题的实质性内容。
我已经实现了一个使用 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="data source=****;initial catalog=IdentityTest;Uid=*****;Pwd=*****;integrated security=False;MultipleActiveResultSets=True;App=EntityFramework"" 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="data source=****;initial catalog=IdentityTest;Uid=****;Pwd=****;integrated security=False;MultipleActiveResultSets=True;App=EntityFramework"" 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="data source=****;initial catalog=IdentityTest;Uid=****;Pwd=****;integrated security=False;MultipleActiveResultSets=True;App=EntityFramework"" 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 的新手,目前正在构建这个模块,以便我可以将它用于我将在今年开发的一些企业应用程序。
【问题讨论】:
-
通常,EF 会为您制作这些,如果您不知道如何更改以使其正常工作,请停止尝试。只需创建一个全新的 EDMX,它就会为您创建一个连接字符串,然后使用这个。
-
我认为我的困惑可能在于使用代码优先方法。我熟悉为数据库优先方法构建和使用 edmx 文件。我还需要先为代码执行此操作吗?只是将它指向一个空的远程数据库?为什么本地数据库不需要这个?
标签: asp.net-mvc entity-framework database-connection asp.net-identity