MVC Switch from Code First to Database first - alter schema without dropping database - entity-framework-5

With Entity Framework it is possible to enable migrations and create migration steps. But is there an intermediate way where it is possible to change the models, and take care of database schema changes yourself? I don't want to drop the database, because there are future production schenario's.
Now - without enable migrations - I use a code first, and when I create another property in a DbSet - lets assume for example in table 'ExistingTable' int NewField {get; set;}
And when in SQL I update my schema with
Alter table ExistingTable add column NewField int not null
the database knows existence of the new field, the Entity Framework / C# knows the property, but when running, there is some hidden check that still want's to drop my database because of the model change.
Question: can I overwrite a certain setting, in such a way that intial 'Code First' can be transformed to database first?

Removing the __MigrationHistory table from the database (Azure) did work fine for me. I made my (simple) database changes myself and published the code. It all runs fine. There is an alternative see EF Code First Migrations Deployment to an Azure Cloud Service. For a simple one-way patch (and no change history needed) removing the __MigrationHistory works fine.

Related

Creating new tables on the fly using sequelize

This is actually the first time I'm posting something here. The problem I have is driving me crazy since I know such functionality exists elsewhere.
I'm trying to find a way to create new tables on the fly on node js using sequelize without having to restart the database (a DB implemented with SQL).
Moreover, I also need to create new associations between those new table and existing ones.
Why do I need this:
I'm creating a crm and one of the functionalities I want to add is to let users create new types of objects that could have fields referencing to other preexisting ones.
And I can't just restart the whole database everytime a user is creating a new object or fields.
If you guys know how to achieve that or maybe advising me to build the database with another language I'll go for it.
Thanks!

How to use existing data with azure?

https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-use-existing-sql-database/
I have been following this tutorial but it ends up creating a new table emptying all the data. How do I make it uses existing values inside an existing table?
The tutorial indeed populate example data using Database.SetInitializer(new ExistingInitializer()); but it wont make new DB.
Make sure you have correct connection string Name=MS_TableConnectionString (step 4 in Exploring the existing database model) so it points out to you existing DB
Edit:
First of all, comment out database seeder Database.SetInitializer(new ExistingInitializer()); and to query your existing data use GetAllMobileCustomers in MobileCustomerController or GetAllMobileOrders in MobileOrderController it will give you IQueryable of MobileCustomer of MobileOrder object, use that object as you wish

What is the best way to add tables to Entity Framework 6 Code First to Existing Database?

I'm developing an ASP.NET MVC 4.5 project using EF 6 Code First to an Existing Database. I would like to create some new tables with foreign key relationships to one of the tables in the dbcontext I've created. I've altered and added columns in that original table, creating several migrations. There is real data in that table.
I would prefer to create the new tables in the database, but don't see how EF would generate a model for me. I can code the model myself, but don't see any documentation about how I would add it to the context class generated by EF. And then the migrations would be out of whack.
So I'm thinking that the best thing to do would be to delete all the migrations, delete the context class and drop the migrations table. Then I could start from scratch with an initial migration. Am I missing some gotcha? Is there a better way?
FWIW to others facing this dilemma, I figured it out. First I got rid of all the migrations, following the 100+ up-voted answer here: Reset Entity-Framework Migrations
Second, I created new the tables and constraints I needed in the database.
Third, I created a new entity in my solution and generated model classes from the database. I changed calls from the old entity to the new entity.The generator overwrote the model for the original table, but since I have all the annotations in version control, it is trivial to paste them in.
If I need to, I can enable migrations again.
Hope this helps.

POCO Class in EF not working as Expected

I have created a DataBase in SQL and created an EDMX in Visual Studio 2012. It automatically created POCO (TT) classes. Everything looks fine.
Now I change the column name of a table. I update the EDMX. Opening the EDMX in XML and everything looks fine.
Question 1
After I ran Custom tool in TT, I see that a new property got created additionally, e.g.:
SQL table name : Student
Column name : sName
In my POCO Class
public int sName{ get; set; }
got automatically created.
Now I change the column name in SQL to
Column name : studentName
My POCO class
public int sName{ get; set; }
public int studentName{ get; set; }
Is this a bug or do I need to do something to fix this?
What should I do to avoid this?
Question 2
Also, if I change the data type of any SQL column and update the model from the DB in my EDMX designer, the Conceptual Model is not updated. How do I go about this?
First, to understand your problem, what you need to know is that the EDMX file is just an XML file that contains 3 different sections:
CSDL: Conceptual schema definition language
SSDL: Store schema definition language
MSL: Mapping specification language
The CSDL contains the entities and relationships that make up your conceptual model. The SSDL describes your DB model and the MSL is the mapping between the 2.
The “Update Model From DB” process will update the SSDL (change everything that is inconsistent with the current DB schema), it will only modify the CSDL in case you’ve added new things to your DB schema.
This is quite a normal behavior since your Conceptual schema may/should differ from your DB schema (unless you want your Domain model to look exactly like a DB model which obviously do not sound as OOP/DDD best practices).
As for #Peru, the solution would be to delete the concerned entity (not the entire EDMX!) and then perform the “Update Model From DB” process.
Hope this helps!
Edit:
There's a tool, not for free, which is a Visual Studio plugin that allows you apply changes made into the DB, both on the CSDL and SSDL files: Huagati DBML/EDMX Tools.
The only "free" solution is deleting the entity (or the right field within this entity) that needs to be updated.
Remember that CSDL is supposed to be maintained by developers and must look like an Object Model rather than a DB Model. Imagine you have setup inheritance between your entities or that you have split 1 DB table to 2 EDMX entities, running "Update model from DB" should not overwrite everything!
Personally, I'd open the edmx file as XML and find the problem node and delete it. The file is pretty easy to understand - don't be scared to at least try it out.
I know I'm a bit late here but there is a relatively simple way to generate your POCOs/update your DbContext from the EDMX designer.
Go to your designer, right click on the empty area, click "Update Model from Database", add/update your tables. This will update your edmx XML. After you've verified that your table has been added to the CSDL, SSDL, and MSL sections, right click on your T4 template in the solution explorer (<Name>.tt, not <Name>.Context.tt) and click "Run Custom Tool" - this will generate POCOs for any updated objects. Note that if you are creating new entities, they will not be added to your DbContext class, you will still have to do this manually by adding the following line at the bottom of your class: public virtual DbSet<Entity_Name_Goes_Here> EntityNames { get; set; }
Only the manual edit worked for a View in my project. (going from smallint to decimal(18,2) )
Open the .EDMX file in a text editor, find appropite section, and manually change the Property Type="..." value.
Once you update the fields in Db, Find the respective model.cs file then delete those fields from the model. now Update the EDMX file (Update Model from Database). It worked for me.

Change connection on the fly

I have a SQL server with 50 databases. Each one has the exact same schema.
I used the awesome Subsonic 2.2 create the DAL based on one of them.
I need to loop though a list of database names and connect to each one and perform an update one at a time.
If there a way to alter how subsonic uses the connection string. I believe I would need to store the connection string in memory that way it can keep changing.
Is this possible?
I tried doing a
ConfigurationManager.ConnectionStrings["theConnStrName"].ConnectionString = updated-connection-string-here;
.. but that did not work
thanks!
Subsonic was designed primarily for one database only.
I've done multiple databases a couple of ways. I had posted a complete sample for creating providers on the fly in the old forums for subsonic. Those posts are gone now. What you can try is set up one provider in the config file (yourProviderName) with a dummy connection string just to initialize subsonic, then set the actual connection string as needed in your code. This only works with one database at a time but I think this is what you needed. If you want to use multiple databases at the same time, and involves more code and some changes to the subsonic 2.x core.
DataProvider provider = DataService.GetInstance(yourProviderName);
foreach(string connString in connectionStrings)
{
provider.DefaultConnectionString = connString;
DataService.Provider = provider;
// ... do stuff
}
I don't think it's easy to fix without creating a SubSonic provider for each database.
Instead I would put the SubSonic-source as a project in the solution and debug my way to the best and most clean place to extend the code wwith some sort of connnection string dictionary list and functionality to set the one you want at a given time.

Resources