将我的Code First Entity Framework创建的数据库移动到Default Membership Provider数据库 [英] Move my Code First Entity Framework created database to the Default Membership Provider database

查看:271
本文介绍了将我的Code First Entity Framework创建的数据库移动到Default Membership Provider数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我已经构建的ASP.NET MVC3应用程序,它有两个数据库。



一个是引用默认成员资格提供者,并在我构建应用程序时自动创建。



另一个是我命名为SchoolContext的数据库,我希望将 SchoolContext 中的所有信息存储在与默认成员资格提供程序中的所有数据相同的数据库中。



这是我的两个连接字符串:

 < add name =ApplicationServices connectionString =data source = .\SQLEXPRESS; Integrated Security = SSPI; AttachDBFilename = | DataDirectory | aspnetdb.mdf; User Instance = trueproviderName =System.Data.SqlClient/> 
< add name =SchoolContextconnectionString =Data Source = | DataDirectory | School.sdfproviderName =System.Data.SqlServerCe.4.0/>

我尝试将 ApplicationServices 重命名为 SchoolContext 然后注释掉我的第二个连接字符串,我可以使用默认成员资格提供程序加载和登录,但是每当我需要排队数据库以获取已存储的信息在原始 SchoolContext 中,我收到错误,指出该模型是不起作用的。 。并且数据不是内置在包含来自默认成员资格提供程序的信息的数据库中。



如果模型更改,我正在使用初始化程序重建数据库: p>

  public class SchoolInitializer:DropCreateDatabaseIfModelChanges< SchoolContext> 

这个工作正常,任何时候我在模型中编辑一些东西,我正在使用两个数据库方案。但是,当我想将两个数据库加入一个时,它不起作用。



我显然不知道如何连接两个数据库,正如我想要做的不正常,我应该如何适当地设置mvc将我的 SchoolContext 放入与默认会员提供商所需信息相同的数据库中?

解决方案

两个潜在的答案:


  1. 意图是尝试使用两个单独的DB
    上下文并执行两者之间的查询。在实体框架中(这个写作),这是
    不可能的,尽管今后可能会考虑
    。有关详细信息,请参阅此信息:
    https://stackoverflow.com/a/8536400/941058



    如果您需要加入信息,您将需要使用LINQ在内存
    中执行此操作。实质上,您需要在任一数据库中存储外键
    信息,并在查询另一个数据库时引用它或


  2. 如果你不是想跨越上下文连接,而是只想使用EF的默认成员资格提供者考虑:
    http://efmembership.codeplex.com/



I have an ASP.NET MVC3 application that I have built and it has two databases.

One is in reference to the Default Membership Provider and was automatically created when I built the app.

The other is a database I have named SchoolContext and I would like all the information within SchoolContext to be stored in the same database as all the data in Default Membership Provider.

Here are my two connection strings:

<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0" />

I tried renaming ApplicationServices to SchoolContext and then commenting out my second connection string and I can load up and "log in" using the default membership provider just fine, but whenever I need to queue the database for information that was stored within the original SchoolContext, I get errors stating that the model is nonexistant, etc. . . and the data is not built within the database that contains the information from the default membership provider.

I am using an initializer to rebuild the database if the model changes:

public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>

And this works fine any time I edit something in the model and I am using the two database scheme. However, it is not working when I want to join the two databases into one.

I apparently do not know how to connect the two databases as what I am trying to do is not working, how should I appropriately set up mvc to put my SchoolContext into the same database as the information needed for the Default Membership Provider?

解决方案

Two potential answers:

  1. If I understand your intent you are trying to use two separate DB Contexts and perform queries that join across both of them. This is not possible in Entity Framework (as of this writing) though it may be considered in the future. See this post for more specifics: https://stackoverflow.com/a/8536400/941058

    If you need to join up information you will need to do it in memory using LINQ. Essentially you'll need to store foreign key information in either database and reference it when querying one or the other.

  2. If you are not trying to cross-context join but instead just want to use the Default Membership Provider with EF consider this: http://efmembership.codeplex.com/

这篇关于将我的Code First Entity Framework创建的数据库移动到Default Membership Provider数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆