EF 4.1 using wrong schema in generated sql - c#

I am new to EF and i can't work out what I am doing wrong. I have used EF 4.1 "database first" to create a model for an existing database (that i can't change). All of the tables that i need in the database are in a particular schema which for this question i will call "my_schema". In the main properties of the edmx designer file i have set Database Schema Name to "my_schema". When i inspect the raw XML of the edmx file it seems to have the correct schema mappings
e.g.
<EntitySet Name="Events" EntityType="MyModel.Store.Events" store:Type="Tables" Schema="my_schema" />
However the SQL generated when i access the Events entity set on the DbContext class is still:
SELECT ....
FROM dbo.Events
I am not sure if it makes any difference but i am using the ADO.net DBContextGenerator to generate my classes.
Does anybody know what I am doing wrong.

OK i have sussed this now and it boils down to my ignorance of how EF works. I was passing my DbContext an ordinary ADO.net connection string which seems to flip it into code first mode. As such any settings and configuration in my edmx model were ignored and it was looking for attributes on the model classes. As soon as i changed it to use an EF string that includes references to the model metadata files it works. Seems obvious now, no idea how i expected it to magically know about model metadata.

Related

Rename class in accordance with SQL Server table in Entity Framework database-first

I have a database in SQL Server like this:
Then I create a model as .emdx in my project by database (database-first approach):
I know class name in .edmx is according to SQL Server table but I need to class without prefix class name (remove OnlineHelp_).
I read many post but they are mostly about code-first and do not work well on my issue.
It would be very helpful if someone could explain solution for this problem.
In the EDMX Designer, you could select an entity, then modify the "Name" property, as shown in the following image (sorry for the italian screenshot)
The property will remain saved after a model refresh from database

How to generate Entity Framework 6.x POCO classes with mappings from an EDMX file?

I'm in the process of converting an extensive EDMX model into POCO classes. I need to go from a Database First approach (EDMX with ObjectContext) to a pure Model First approach (DbContext with no EDMX file). I need to use the latest Entity Framework stable version: 6.1.1.
I've tested some approaches:
Adding a the EF 6.x DbContext Generator code generation item by right-clicking the blank space in EDMX designer. This works fine, but it doesn't add any mappings. With this approach I have to still use the EDMX file. It's not full Code First.
Using the EF 5.x DbContext Fluent Generator for C#. This triggers an exception in design time. I'm not being able to use it. I don't know if that's because my VS Entity Framework tools are already updated to 6.x. Using the alternative TT in the comments, that suggests that it would work with EF 6.0 also doesn't help.
Using the EntityFramework Reverse POCO Generator. This is the worst because it won't consider any of my classes and navigation properties renames.
Using the Entity Framework Power Tools Beta 4. Again, it only supports generating from the database, not from the EDMX file.
My requirements:
I need the input to be the EDMX file, not the database.
I need the output to be a full Code First approach with Fluent mappings.
I need all my navigation property names defined in the EDMX to be considered because otherwise it would break a large codebase, even more then migrating from ObjectContext to DbContext will break.
What do you think would be a good option for me to go?
Well i don't think there is an easy one click solution to this.
Underneath you edmx files. You have two more files available besides the xx.Designer.cs and xx.edmx.diagram.. called xx.Context.tt and xx.tt where xx is the name of your edmx model.
These are t4 templates which genrate your dbcontext and poco objects. All your poco objects would be created underneath your xx.tt files and dbcontext underneath your xx.Context.tt files.
You now have to moves these into separate files. This is much easier if you are using EF6. and the file generated are already using DbContext and not ObjectContext.
I faced a similar case and I used Entities to DTO's generator.
Although its purpose is to generate DTO's, however, I believe it can help someone in you case.
https://entitiestodtos.codeplex.com/

Cannot update Entity Framework model after schema name change

I am on a project that has an EF model that was created with a schema of 'dbo'. At some point in time the schema was changed to something more meaningful. However, try as I may I cannot now update from the database with the newly named schema as all the entities create new objects instead of updating the current ones. For example instead of Person, I now get and Person1. I have tried to edit connection strings as well as the EDMX file but nothing seems to be working.
Is there anyway to get my model to update from the database with the renamed schema?
I am using EF 6.0 and .Net 4.5.

What is the difference between 'Database First' and 'Code First to Existing Database' in Entity Framework

I have just watched the MSDN video about the new 'Code First to Existing Database' functionality in EF6.1
http://channel9.msdn.com/Blogs/EF/Code-First-to-Existing-Database-EF6-1-Onwards-
However, being new to EF, this appears to be generating POCO Model classes from a DB, which is exactly what I thought 'Database First' does (and I understood 'DB First' to be the opposite of 'Code First')
What's the difference? They are both 'database first'!
Code First is sort of a misnomer... What it should really be called is "Code Based Model" vs "XML Based Model".
Code first creates an in-memory model based on attributes on classes and/or fluent mappings in code.
Database and Model first create an in-memory model based on a .EDMX file, which is then used to generate classes.
ie. Code first uses the code as its model. Database and Model first use the EDMX file as it's model. Code First to existing database just generates code first classes (attributes and/or fluent mapping), while database first generates an EDMX file which then generates classes via T4 templates.

Code First vs. Database First

I created an Entity Framework model based on an existing database, then generated the POCO entities from the model. The connection string in my web.config isn't Entity Framework, it's just the standard connection string (it's missing the CSDL, SSDL, MSL references).
I can compile my application, but when I run I get this error:
Code generated using the T4 templates for Database First and Model
First development may not work correctly if used in Code First mode.
To continue using Database First or Model First ensure that the Entity
Framework connection string is specified in the config file of
executing application. To use these classes, that were generated from
Database First or Model First, with Code First add any additional
configuration using attributes or the DbModelBuilder API and then
remove the code that throws this exception
My question is, where in my code does it realize the POCOs came from auto generation, and how can I get it to behave like Code First? I don't want to reference the CSDL etc in my connection string.
If the connection string has the metadata, EF thinks it is Model First or Database First. If it is a plain connection string, EF thinks it is Code First. However, if you want to start out doing model first but make EF think you are really doing code first (which is what you are doing), make sure you are using the DbContext code generator, not the default one. Code first POCOs are really that--"plain old c# objects"-- no special database aware or change tracking stuff in them at all. To use the DbContext code generator, right click on your model diagram and choose "Add new code generation item..." then select the ADO.NET DbContext Generator. Also, depending on how you named your primary and foreign keys and/or whether they are more complicated than just simple int IDs, you will probably need to fill in some code to map the relationships between your objects in the "OnModelCreating" method in your context. Delete the line throw new UnintendedCodeFirstException(); and replace it with your mapping code. Otherwise EF may not be able to figure out all the relationships (remember there's no metadata for it to rely on).
Hope this helps.
You need the following in your config file:
<connectionStrings>
<add name="<The name of your class>"
connectionString="metadata=res://*/<test>.csdl|res://*/<test>.ssdl|res://*/<test>.msl;provider=System.Data.SqlClient;provider connection string="data source=<your source>;initial catalog=<your db>;persist security info=True;user id=<your user id>;password=<your password>;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
I'm using Database first and resolved this by copying the EDMX generated connection string to the app.config of my startup application. One already existed but apparently they were different

Categories

Resources