Entity Framework Load/Save Data - c#

I am using SQLite with the Entity Framework in a C# Windows Form application. I have chosen to use an in-memory database. I'm trying to load a database file that exists on disk into memory. I'll also need to save what's in memory to the disk.
This seems like a relatively simple task but I can't find an elegant solution to it without hard-coding table creation and updating the queries whenever I change something (I.E: Not feasible)
If anyone can provide assistance, it would be greatly appreciated.

Actually, you may easily use SQLite in-memory database with entity framework.
Look at the following threads as an initial guideline:
In Memory Sqlite Entity Framework Context
Integration Testing Entity Framework code first with in-memory database

Related

Loading and persisting an In-Memory SQLite database with EF Core

I'm doing some research for a new project and I'm trying to determine if it's possible (and advisable) to load a SQLite database into memory, perform CRUD operations against it, and persist it back out.
I've seen many examples of utilizing SQLite databases (in memory) for unit testing, and in all of those examples, the data is just trashed in the end - this is NOT what I wish to do.
I'm going to likely use Microsoft SQL Server to manage the overall site data and act as a storage engine (users and credentials and their associated SQLite databases, etc).
When a user selects a SQLite database in the UI, I would like to load it into memory on the server, allow the user to operate against it, and then persist it back to the storage engine (SQL Server) without needing to save a .db file to the filesystem.
I'm comfortable with aspects of EF Core + SQL Server and SQLite (against the filesystem). But what's new to me is the idea of operating against a SQLite database in memory.
So my questions are:
Is this possible with EF Core?
If so, how would I configure my SQLite DbContext class to accomplish this?
Are there any major downsides to this?
Thank you
Is this possible with EF Core?
Yes. Why not? SQLite essentially treats in-memory database the same as any other. There are particular considerations, but you essentially insert and query data in the same way.
If so, how would I configure my SQLite DbContext class to accomplish this?
Once again, refer to documentation. You would need to provide a specific connection string. (Not to discourage SO questions, but if you're going to research and test this you really should research information available on sqlite.org. It is has great, thorough documentation--at least compared to many open-source projects... sometimes a bit scattered, but still accessible.)
Perhaps more complicated than specifying an appropriate connection string is actually loading an existing database file into memory. The default, basic behavior is to only create an empty database in memory. There are multiple ways to load the data, and this question has some useful answers.
Are there any major downsides to this?
You have apparently already identified some of the downsides, but probably no more than any project which needs to merge/synchronize databases. There is no short answer to that question and it is much too broad for Stack Overflow.
You specifically mention syncing data to an SQL Server without saving the data to a disk file. You will certainly have to perform a series of queries form sqlite, massage the data into a corresponding update statement for SQL Server, then execute that on the server. Perhaps there are third party tools to do that same thing for file-based databases, but I suspect that you'd end up performing the same operation with a disc file anyway.

Migrating data from 2 different SQL Server databases using console app

I have 2 web applications which are let's say Legacy1 and New1. I have separate DbContext for both applications and separate databases.
Now I want to migrate data from the Legacy1 database to the New1 database. I cannot simply use copy database or export database options in SQL Server. Entities are different and I need to incorporate some logic while migrating data.
I have decided to use .Net Core Console (.Net Framework) project to do this. Do I have to add both DbContexts to this project? Also all the entities which I want to map and do the migration or is there any other way to achieve this.
Finally i found a simple approach to achieve that. I used EF DB First approach to generate edmx files for source and target databases. Which gives me access to corresponding Entities as well.
I Queried source database using source dbcontext and manipulated data as per the requirements and inserted into target database using target dbcontext. I used AutoMapper to map source entities to Target entities.
It was much simpler than i thought earlier. Hope it helps others having similar scenario.

Write data to two SQL databases from Entity Framework in c#

I am having an issue with my website(ASP.NET, c#, SQL, Code-first Entity Framework).
I have a project with an attached SQL database generated from code first entity framework.
Now I have imported another SQL database using model first database which looks almost same but the table and column names are different.
So now I would like to write data to two databases at the same time with just one click from my web application.
The newly attached database will be a backup and we should write data to both databases at the same time.
Any suggestions would be highly appreciated.
Thanks
As others have suggested, you need to do the mapping yourself, but one thing I would like to add, you may need to wrap your SaveChanges() into a transaction, you may find steps here: https://msdn.microsoft.com/en-us/data/dn456843.aspx

Application DAL design

Hello and thanks for looking.
I have a DAL question for an application I'm working on. The app is going to extract some data from 5-6 tables from a production RDBMS that serves a much more critical role in the org. What the app has to do is use the data in these tables, analyze, apply some business logic/rules and then present.
The restrictions are that since the storage model is critical in nature to the org, I need to restrict how the app will request the data. Since the tables are relatively small, I created my data access to use DataTables to load the entirety of the db tables on a fixed interval using a timer.
My questions are really around my current design and the potential use of EF or LINQtoSQL
Can EF/LS work around the restrictions of the RDBMS. Most tutorials I've seen, the storage exists solely for the application. Can access to the storage be controlled and/or can EF use DataTables rather than An RDBMS?
Since the entirety of the tables are going to be loaded, is there a best practice for creating classes to consume the data within these tables? I will have to do in memory joins and querying/logic to get at the actual data I need.
Sorry if I'm being generic. I'm more just looking for thoughts and opinions as opposed to a solution to my problem. Please done hesitate to share your thoughts. Thanks.
For your first question, yes Entity Framework can use a existing DB as it's source, the term to search for when looking for Entity Framework tutorials on this topic is called "Database First"
For your second question let me first preface it with a warning: many ORMs are not designed around using it to load the entire data table and do bulk operations on them, especially if you will be modifying the result set and pushing the data back to the server in large quanties. The updates will be row based not set based because you did the modifications in C# code, not in a T-SQL query. Most ORMs are built around the expectation that you will be doing CRUD operations on the row level, not ETL operations or set level CRUD operations (except for Read which most ORMs will do as a set operation).
If you will not be updating the data, only pulling out using Entity Framework and building reports and whatnot off of the data you should be fine. If you are bulk inserting in to the database, things get more problematic. See this SO question for more information.

Entity Framework code first with Access database

Is there any way I can connect EF with an Access database using code first
so that my POCO classes can create the tables in the Access database and everything will save on that file?
I'm afraid you are out of luck, there is no EF provider for MS access (or i never heard of one exists). You could try with ALinq. It's not a EF provider, but it does provide LINQ support for MS access database. Not free a solution and I never tried it, but maybe it will work for you.

Categories

Resources