How to use Entity Framework 4.0 with Xml or in-memory Storage (non-SQL) - entity-framework

How do I specify Xml or just in-memory storge for Entity Framework models? The connection string requires a provider (usually a SQL provider string). But it won't let me omit the provider.
I realize I could completely throw away the designer generated objects and go pure POCO, but then I'd have to implement my own serialization layer (could do that, but it's overkill for the tiny project I'm working on).
Is there built-in support in EF 4.0 for this that I'm missing or do I just need to go the pure POCO route and discard the designer experience entirely :(

If you want to store data in Xml or memory you should probably not use EF. EF is designed to work with relational databases.
See also: Entity Framework with XML Files
For storing data in memory use System.Runtime.Caching
For storing data in xml files see: http://msdotnetsupport.blogspot.com/2007/04/reading-and-writing-xml-files-using-c.html

This is a good way to do what you're probably thinking.
Use a SQLite db as the backing store. That way you get you're single local file and you can still use almost all of EF.
http://dotnet.dzone.com/news/sqlite-entity-framework-4

Related

What's the point of running an EF migration when you can SQL directly in database?

How to create View (SQL) from Entity Framework in ABP Framework
Not allowed to post comments because of reputation. Just trying to get more information on connecting a database to an Entity Framework, without having to switch to a code-first development style. View selected answer's response (he told the OP to basically do the same thing he was going to do in the DB but with EF, and then added an extra step where EF "...ignores..." the previous instructions...
I want to create tables and design database directly in SQL, and have the csharp library just read/write the table values (kind of like how dapper function where it isnt replacing your database, just working along side of it).
The tutorials don't talk about how to integrate your databases with your project. It either brushes over the subject, ignores it completely, or discusses how to replace it.
I don't want to do any EF migrations (i dont want/need to destroy/create database everytime i decide to run, duplicate, or transfer project). Any and all database back-track (back-up/restore) should be done with and thru SQL (within my work environment).
Just to be clear on exactly what i'm trying to learn:
How does somebody who specializes in database administration (building database schema, managing and monitoring data, and has existing database with data established) connect to project to fetch data (again, specifically referencing Dapper's Query functionality).
I want to integrate and design micro-services, some may share the same database connection or rely on another. But i just simply want to read data in a clean strongly-typed class entity, and maybe deal with insert/update somewhere else if i have to.
I would prefer to use Dapper instead of EF, but ABP is so heavily integrated with EF's design, it's more of a headache to avoid it, than it is to just go along with.
You should be able to map EF under ABP the same way as any other project using DB-first configuration.
The consistent approach I use for EF: (DB-First)
Define entities to match the table/view structure.
Define configuration classes extending EntityTypeConfiguration<TEntity> with the associated ToTable(), HasKey(), and any HasMany/HasRequired/HasOptional for relationships as needed.
In DbContext.OnModelCreating: modelBuilder.Configurations.AddFromAssembly(GetType().Assembly); to load all entity configurations. (assuming DbContext is in the same assembly as the models/configurations Substitute GetType().Assembly to point at the entity assembly.
Turn off Migrations. In DbContext constructor: Database.SetInitializer<MyDbContext>(null);
EF offers a lot more than simply mapping tables to classes. By mapping relationships between entities, EF can help generate optimized queries for retrieving data across those related entities. This can allow you to flatten data structures without returning unnecessary data, replace the need for views, and generally reduce the amount of data coming across the wire from the database to the application server.

Efficient write only (CREATE/UPDATE) object persistence in EF6 - stored procedures?

It seems a lot has changed in EF (code first) since many of the questions on SO were written... many refer to EF4 and I am using EF6, I know stored procedures are a new feature in EF6 for instance.
My application periodically calls an Xml web service and simply dumps the objects in a database; I have a C# object for each Xml type which maps directly to one DB table using EF.
Objects may be created or updated depending if they are already in the DB. My application has no interest in the content of the objects and should not keep them in memory as it may be running for weeks at a time.
What is an efficient way to Create-or-Update objects in this scenario? I am imagining that my DbContext will be created (and disposed) each time the web service is polled, so it will never already know what objects are in the DB. In a non-EF world I'd probably create a stored-procedure CreateOrUpdate(...) so I wonder if there is a simple parallel.
I do not need to use stored procedures with EF but it sounds like it might be a nicer idea. And since we're not used to EF and not all our modules are using it, haveing the stored-procedures in the DB in case someone else wants to use them seems useful.

How entity framework reveals properties and types of a code first entity in runtime?

I just want to know how Entity Framework internally works to reveal properties and their types in runtime, particularly in case of Code-First approach, where there won't be system generated code. Can some body give some heads up? I don't think System.Reflection was being used implicitly?
Code first was first presented to developers as part of the EF Feature
CTP1 in June 2009 with the name “code only.” The basic premise behind
this variation of using the EF was that developers simply want to
define their domain classes and not bother with a physical model.
However, the EF runtime depends on that model’s XML to coerce queries
against the model into database queries and then the query results
from the database back into objects that are described by the model.
Without that metadata, the EF can’t do its job. But the metadata does
not need to be in a physical file. The EF reads those XML files once
during the application process, creates strongly typed metadata
objects based on that XML, and then does all of that interaction with
the in-memory XML.
Code first creates in-memory metadata objects, too. But instead of
creating it by reading XML files, it infers the metadata from the
domain classes (see Figure 1). It uses convention to do this and then
provides a means by which you can add additional configurations to
further refine the model.
ModelBuilder will now take this additional information into account as
it’s creating the in-memory model and working out the database schema.
By Julie Lerman

In scenario, where only stored procedures are used for ASP.NET MVC 4 application - is better ADO.NET or EntityFramework 5?

I want to know your opinion.
In scenario, where only stored procedures are used for data manipulation - is better to use standard ADO.NET or Entity Framework 5.
The only (and main) reason for using EF are strongly typed classes (generated complex classes), that can be used as a model in ASP.NET MVC 4. Updating complex types could be simpler in EF compare to ADO.NET.
Reason for using ADO.NET is making communication with database more simply.
Thank you for your opinion.
You could also look at e.g. Dapper-Dot-Net (or some of the other "micro-ORM") which is based off "raw" ADO.NET, but also offers conversion to nice .NET objects (the main EF benefit in your case, I believe) from stored procedure results

Have anyone used Entity Framework with code first approach mixed with Edmx file?

I'm currently assign to a project where their legacy system is designed in a horrible way and it's been too much focus on database design. I trying to put together a new design where the customer can migrate the legacy system bit by bit.
They are currently using EF 4.1 BUT not code first approach with entity descriptive/mapping is located in an edmx file. They do Reverse engineering everytime to want to extend the model (First make changes in database, then reflect them upwards to Model layer through a custom tool).
What I would like to know, if anyone has used BOTH edmx and code first approach with mapping classes. And is there drawbacks to know about?
You can use EDMX and code mapping together only if you have separate context type for each approach (you cannot mix approaches in single context type). That is probably the biggest disadvantage because it leads to more complex code and maintenance.
For example if you need to have some entity in both contexts types to use it with both new and legacy code you must maintain its mapping twice. You must also be very careful about not duplicating entity class itself = your code first must use class generated by custom tool for EDMX but this will not be possible if they are not using POCOs in current solution.
Another problem will be database integrity. If you will need to save changes to both context types in single transaction you will have to use TransactionScope and distributed transaction = MSDTC (each context instance will handle its own database connection).
If you are sure that whole system will be migrated you can probably think about using code first instead of EDMX (but be aware that code first mapping and DbContext generally offers more limited feature set). If you are not sure that you will be able to complete whole migration don't even think about using code first because leaving system in the state where half uses code first and half EDMX will make everything only worse and much more horrible.
Being sure is little bit theoretical because in SW development the only think you can be sure about is that requirements / situation will change. It means that migration should be very carefully considered.
I also was struck with this problem. What I found was that you can model the database and "generate the database from the model" in a "Ado.NET Entity model Project".
But you can not create stored procedures in that project, What only you can do is you can import the stored procedures from the server.
But if you do not want to create stored procedures on the server, you can create another project on VS, "SQl CLR Database Project" and you can code your stored procedures and tigers in that project and deploy them to the server.
then you can again import these stored procedures from the "Ado.NET Entity model Project" by "Update Model From Database".
Like wise you can develop your server project using both approaches(Code first and Model first)
Hope this will add something more :)