3

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.

How can I use the EF model without a connection?

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

5
  • You want to use in memory database? If your version of EF supports it, use that. Otherwise, abstract away your data layer so you can swap out DB connection with in memory (or local embedded database). Commented Apr 2, 2015 at 18:58
  • What do you mean by using EF without a database? Commented Apr 2, 2015 at 19:02
  • Yes, I want to use in memory. Add items to tables and such without saving to the DB. I just can't figure out how to create an instance of my EF object without having a valid connection string. Commented Apr 2, 2015 at 19:30
  • Check out LocalDB msdn.microsoft.com/en-us/library/hh510202.aspx. No network connection required. Commented Apr 2, 2015 at 20:01
  • Thanks for the LocalDB idea, I will look into that. Commented Apr 3, 2015 at 0:45

2 Answers 2

3

Instantiating DbContext doesn't open connection to the database. 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.

The other question is: does you application can really work without using a database? And what are you trying to achieve?

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, it works without the DB. I just changed it to use EF so that I wouldn't have to translate everything when I want to save/load some prefigured data. How can I do a new of the EF object without trying to connect to the DB?
What do you exactly mean by "do a new of the EF object"? Instantiate instance of some entity? If so, just use "new" keyword, as for any other class.
See the code I added for SimRunnerEntities. I need to do a new SimRunnerEntities, but obviously it gives me an error if I don't have a connection string named SimRunnerEntities in my .config file.
Actually, you cannot use any methods from SimRunnerEntities, as it will require database connection. You want database disconnected level - EF doesn't support it, as I am aware. To make it work without connection, you will need to add custom level of abstraction, and some logic (a lot of and really complex) to manually save a copy of database and store all commands, that were performed on it, to replicate them in future, when connection exists. Maybe there are some third-party solutions for such cases, but it will be definitely a pain, especially when trying to get everything in sync.
1

Yes, you can do that.

In example, I have a solution which work with that architecture layout:
Project A - Server Side (WCF)
Project B - Class Library which Contain EF model + DB connection
Project C - Client side(WPF)

A reference to B,
A is WCF and hard copy the configuration from B so it can access the database using the DBContext.

C reference to 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.


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


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.

A: sure, project B configuration file (app.config) is simply not being used.
I just have it there for archive purpose or for copy paste..
I did copy and paste it to 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.

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.

What I believe might be your next question is how to convoy data from WCF to client side - making the entities "trackable"? and for that I created a whole DTO Layer.. however that is beyond of your original question scope :)
Regarding the simple question can you use the Entities without using the dbcontext to manage them the answer is: Yes.

6 Comments

Can you explain a bit more. If your Project B has a DB connection how does this solution work without a DB connection. In my solution I have 2 projects one for the data including an EF obj and the other for my User interface. In the UI I want to instantiate a data object but fails without the valid connection string because it include a EF item.
Sure, I add more content in the answer to explain a bit more. feel free to ask for more if you need more info.
Can you post the constructors of your entity framework object like I did.
My constructors are complicated since I created a DTO layer which communicate with my Entity Objects, but in general every EF object is partial class, so I just add another code file in the same name with partial keyword and add whatever I need to the object, in my case I derive a generic abstract class which handles the DTO needs.. it adds CopyFromDto, CopyToDto and other methods which handle delete and so on.
Sounds kind of complicated but very useful. You should do an article on it and post a simple example of how to do it. I am sure many would find it useful.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.