简体   繁体   English

在没有数据库连接的情况下使用实体框架

[英]Using Entity Famework without a DB connection

I have an application that I tied to a DB for loading and storing data. 我有一个与数据库绑定的应用程序,用于加载和存储数据。

However I don't want to require the user to be on the network and have access to the DB. 但是我不想要求用户在网络上并且可以访问数据库。 It just can't load or save without a connection. 没有连接就无法加载或保存。

I would like to instantiate it without a db connection (just use it in memory), but all constructors use the DB. 我想在没有数据库连接的情况下实例化它(只在内存中使用它),但是所有构造函数都使用数据库。 I would prefer not to modify the entity framework generated .cs file in case I need to update it again from the DB I don't want to wipe out my changes. 我宁愿不要修改由实体框架生成的.cs文件,以防万一我需要从数据库中再次更新它,而又不想消灭所有更改。

How can I use the EF model without a connection? 没有连接的情况下如何使用EF模型?

public partial class SimRunnerEntities : ObjectContext
{
    #region Constructors

    /// <summary>
    /// Initializes a new SimRunnerEntities object using the connection string found in the 'SimRunnerEntities' section of the application configuration file.
    /// </summary>
    public SimRunnerEntities() : base("name=SimRunnerEntities", "SimRunnerEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    /// <summary>
    /// Initialize a new SimRunnerEntities object.
    /// </summary>
    public SimRunnerEntities(string connectionString) : base(connectionString, "SimRunnerEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    /// <summary>
    /// Initialize a new SimRunnerEntities object.
    /// </summary>
    public SimRunnerEntities(EntityConnection connection) : base(connection, "SimRunnerEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

All SimRunnerEntities constructors use some sort of valid connection 所有SimRunnerEntities构造函数都使用某种有效的连接

Instantiating DbContext doesn't open connection to the database. 实例化DbContext不会打开与数据库的连接。 DB connection will be opened after first query is made, where you select something or so. 进行第一个查询后,您将在其中选择某些内容,然后将打开数据库连接。 So if you are not modifying or reading any data, you are completely safe working without access to your database. 因此,如果您不修改或读取任何数据,则无需访问数据库即可完全安全地工作。

The thing is, that you can't really do anything with EF DbContext without triggering database, except using it's entity classes for some custom purpose. 事实是,除非使用EF DbContext的实体类来实现某些自定义目的,否则您实际上无法使用EF DbContext进行任何操作。

The other question is: does you application can really work without using a database? 另一个问题是:您的应用程序真的可以在不使用数据库的情况下正常工作吗? And what are you trying to achieve? 您想达到什么目标?

Yes, you can do that. 是的,你可以这么做。

In example, I have a solution which work with that architecture layout: 例如,我有一个可以使用该体系结构布局的解决方案:
Project A - Server Side (WCF) 项目A-服务器端(WCF)
Project B - Class Library which Contain EF model + DB connection 项目B-包含EF模型+ DB连接的类库
Project C - Client side(WPF) 项目C-客户端(WPF)

A reference to B, 对B的引用
A is WCF and hard copy the configuration from B so it can access the database using the DBContext. A是WCF,并从B硬拷贝配置,因此它可以使用DBContext访问数据库。

C reference to B, C引用B,
C is WPF project, the entities being filled from A using WCF and already have the goodies like INPC which already auto generated by EF. C是WPF项目,使用WCF从A填充实体,并且已经具有诸如EF可以自动生成的INPC之类的东西。


The only thing I had to add in the client side is a custom class container which have list of properties of entities type (ie. List Users {get;set;} and etc..) 我唯一需要在客户端添加的是自定义类容器,该容器具有实体类型的属性列表(即List Users {get; set;}等)。


Further explanation according to comment request: 根据评论要求的进一步说明:

Q: Can you explain a bit more. 问:您能再解释一下吗? If your Project B has a DB connection how does this solution work without a DB connection. 如果您的项目B具有数据库连接,那么在没有数据库连接的情况下该解决方案如何工作。

A: sure, project B configuration file (app.config) is simply not being used. 答:当然,根本没有使用项目B的配置文件(app.config)。
I just have it there for archive purpose or for copy paste.. 我只是将其保存在此处以用于存档或复制粘贴。
I did copy and paste it to Project A app.config. 我确实将其复制并粘贴到Project A app.config中。

Q: In the UI I want to instantiate a data object but fails without the valid connection string because it include a EF item. 问:在UI中,我想实例化数据对象,但是由于没有有效的连接字符串而失败,因为它包含EF项。

A: Regarding the instantiate an object.. simply as if you instantiate any other object: 答:关于实例化一个对象..就像您实例化任何其他对象一样:
var entityX = new MyEntity{fill props code here};
It not requires me to use the Dbcontext to do that. 它不需要我使用Dbcontext来执行此操作。

What I believe might be your next question is how to convoy data from WCF to client side - making the entities "trackable"? 我相信您的下一个问题是如何将数据从WCF传送到客户端-使实体“可跟踪”? and for that I created a whole DTO Layer.. however that is beyond of your original question scope :) 为此,我创建了一个完整的DTO层。.但是,这超出了您最初的问题范围:)
Regarding the simple question can you use the Entities without using the dbcontext to manage them the answer is: Yes. 关于一个简单的问题,您是否可以使用实体而不使用dbcontext来管理它们,答案是:是的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果有匹配项,则使用实体名誉列表 - If any matches any in list using Entity Famework 为单元测试目的实例化没有数据库连接的空实体模型 - Instantiating an empty Entity Model without a DB connection for Unit Testing purposes Entity Famework 从对象创建列 - Entity Famework creates columns from objects Entity Famework:在带有日期格式的 Linq 问题中选择 - Entity Famework: Select in Linq Problems with Date formats 在没有数据库连接的情况下使用实体框架类 - Using Entity Framework classes without database connection 实体Famework数据服务作为Windows服务如何更改maxReceivedMessageSize - Entity Famework Data Service as a Windows Service how to change maxReceivedMessageSize 实体框架的第一个数据库连接缓慢 - Entity Framework slow first db connection 实体框架4.1与外部数据库的连接失败 - Entity Framework 4.1 Connection to external DB fail 使用 Powershell 凭据将实体框架数据库连接转换为使用 Azure 身份 - Convert Entity Framework DB Connection to use Azure Identity using Powershell Credentials 实体框架迁移而无权访问主数据库 - Entity Framework migrations without access to master db
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM