I'm forced to use a legacy SQL Server 2000 database for an EF-based app I am writing. The tables already exist, so I need to generate the Entities layer. I can do this in VS2010 using MySQL and recent versions of SQL Server, but not 2000.
To get around this, I followed some tutorials that explain how to generate csl, msdl and ssdl files using edmgen.exe.
That works fine. I now have those files in e.g. c:\temp.
Please can someone tell me what to do with these files? I want to Entity Framework-ify a simple console application that I have written. Can I somehow create an 'ADO.NET Entity Data Model' from these files so I end up with what I would have if I had used VS2010 all along?
Thanks
I got it working, here is what I did
1) Use edmgen.exe to generate the files mentioned in my question
"C:\windows\Microsoft.NET\Framework\v4.0.30319\edmgen.exe" /mode:fullgeneration /c:"Data Source=<your_server_here>; Initial Catalog=<your_catalog_here>; UID=<username>;PWD=<password>" /project:<vs2010_project_name> /entitycontainer:<project_name>Entities /namespace:<project_name>Model /language:CSharpEntityFramework
2) Follow these instructions "How To: use your existing CSDL/MSL/SSDL files in the Entity Designer CTP2" - its actually for VS2008 but it worked for me too.
http://blogs.msdn.com/b/dsimmons/archive/2007/12/07/how-to-use-your-existing-csdl-msl-ssdl-files-in-the-entity-designer-ctp2.aspx
3) I had a further problem in that my legacy db had no primary keys so I followed these instructions which also worked
http://pratapreddypilaka.blogspot.in/2012/04/entity-framework-adding-datatable-with.html
These files are referenced in EF's connection string and EF use them at runtime to build a mapping but it can still not work because SQL Server 2000 is not supported by EF (at least EF 4.0 and newer). EF can generate SQL not supported by SQL Server 2000 and you will get exceptions at runtime.
Related
Using Visual Studio 2015 with SQL Server 2012. I've been happily using code first existing database to generate my data layer with no problems, until last week. For some inexplicable reason VS will not generate the Entity classes from my database tables (any solution, any projects, different databases and SQL instances).
The DbContext inheriting model class is generated containing the public DbSet properties for each table, also the OnModelCreating method which contains an entity<>.Property assignment for each field in each table.
Basically VS can talk to the database, read the schema, map and create everything except the entity classes. I am working on Win Server 2008R2 and applied a number of updates prior to the issue. I have since removed the updates, uninstalled then reinstalled VS but to no avail. Searching the web does not highlight anyone else who has encountered this. Can anyone help please? (VS 2013 is working fine).
Maybe I'm defeating the purpose of Code First but for one reason or another let's look at the problem.
I'm working in EF Code First. My 'Cars' POCO has a "Make" field,.. that matches the "Make" column in the db. Now I come along and I manually rename the column in the db to "Manufacturer". How can I force EF to catch up and update/ rename the POCO?
At the moment I'm using EntityFramework 6.1.3 and VS 2010.
The only solution I could find to this problem of the database being out of sync with EF on the code side was to delete all of the MIGRATIONS and the ENTITIES poco classes, and on the db side delete the _migrations table. Then go back to your application and add to your project a new "Code First from Existing Database".
This solved the problem for me easily and will allow me to work with Code First AND SQL Server Management Studio (as I like to do (until I get better at EF)).
I know it's a bit of a work-around but it'll get you out of jail if you find yourself in this situation.
I've tried to use Entity Frameworks Power Tools Reverse Engineer Code First for a SQLServer database and received the following error:
System.ArgumentException: The modelEntityContainerName parameter 'testContext' contains characters that are not valid.
at System.Data.Entity.Design.EntityModelSchemaGenerator..ctor(EntityContainer storeEntityContainer, String namespaceName, String modelEntityContainerName)
at Microsoft.DbContextPackage.Handlers.ReverseEngineerCodeFirstHandler.ReverseEngineerCodeFirst(Project project)
Is there something I can do to continue or avoid this error or work around it.
Thanks.
I had the same problem. My database was named sots-version-005, and the code was not escaping the name. The answer is to rename the database, and hopefully you are in a development environment where that is easy to do.
I used SQL Management Studio, right-clicked on the database, and renamed it 'sots5', eliminating all characters except alphanumerics.
I am currently working with VS 2013 and Entity framework version 6.1.2 and ran into the same error.
I found this post by ErikEJ on Entity frameworks codeplex site:
http://entityframework.codeplex.com/workitem/898#CommentContainer9
"#Greg - This issue is not fixed in Power Tools, but is fixed in the version 6.1 Tooling (Code First from Database) (just tested with SQL Compact)"
So, currently PowerTools cannot be used for this.
However Entity framework 6 itself supports this.
Just follow the workflow described here in this video:
http://msdn.microsoft.com/en-us/data/jj200620
You add the entity data model item to the project and then choose "Code first from database".
Please note that the generated classes are a little bit different from what Power Tools would have created. Power Tools only uses Fluent API when configuring the database.
The EF wizard on the other hand uses Data annotations by default and uses Fluent API only when necessary.
However it is also possible to change the template files so that only Fluent API is used.
I have a MVC3 website I have been playing with, and the database is quite well populated. I need to change the underlying models, but of course, the standard approach would drop all data. Having the CREATE SQL (so I can ensure all fields/relationships are in line with the models), and the calculated hash (so EF thinks the models match the database) would allow me to manually make changes to the database.
Is there a way to interrogate a DataContext (or some other object) to:
1. Get the SQL it would use to generate the dataschema; and
2. Get the ERM Metadata hash
I have considered some other migration options, but just want to explore this avenue.
Edit: This is EF4.1, and is running against SQL 2008 R2 if that is of any relevance.
Thanks
Andrew
You can do it with EF Migrations. You'd need to upgrade your project to EF 4.3. You'd use the "Update-Database -Script" ps command.
Here is a link to the ASP.Net team blog on it: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx
Pluralsight also has a course on it. They also have a free trial. http://www.pluralsight-training.net/microsoft/Courses/TableOfContents?courseName=efmigrations
I am creating a project using Entity Frameworks 4 and self tracking entities. I want to be able to either get the data from a sql server 2008 database or from sql server compact database (with the switch being in the config file). I am using the repository pattern and I will have the self tracking entities sitting in a separate assembly.
Do I need two edmx files? If so, how do I generate only one set of STE's in the separate assembly? Also do I need to generate two context classes as well? I am unsure of the plumbing for all this. Can anyone help?
Darren
I forgot to add that the two databases will be identical and that the compact version is for offline usage.
Just to follow up on this. In the end I had to maintain two separate edmx files, one for sql server and one for compact. The main reason being that compact 3.5 does not support auto identities (as mentioned above by Zeeshan). This in turn led to two context classes. In the context class for sql server compact I had to put code to check for insertions, query the database for the latest id and increment it manually before saving.
Thankfully with the release of compact 4.0 this no longer applies as it supports auto id and you can indeed use just one edmx file.
Darren
You do need the edmx file as long as the schema is exactly the same. just change the connectionstring and everything would work seamlessly. Though i am not sure how u are saying that schema is same when compact edition does not support identity concept and full blown sql server does. So if you are using features specific to sql server that's not available in compact, then you would get runtime errors.