How to update the Entity framework after adding a new Table to the database in MVC - sql-server

Alright first i apologize if i used any wrong terms.
my problem is that i created a database then i made the models out of it (database first approach) afterwards I've added a new table called "accounts" for example.
of course i cannot use it in the controller like
var x = db.accounts.Tolist();
because its not included in the models.
i tried to "update the model from the database" from the model design panel but it didn't create any new class for the new table..
any idea ?

You need to make sure that on the "Add" panel, you select the new table you want to add:
Sometimes I find that when I have problems it's easiest to just delete all the tables from the edmx and re-add them all, or even just to re-create the edmx in its entirety.

Related

Rename table or column in SQL server without breaking existing apps

I have an existing database in MS SQL server and want to rename some tables and columns because the names currently used aren't accurate to what it represents.
I have multiple web and desktop applications that access the database, using Entity Framework (code first). Too many to update in one go and cannot afford for all apps to start working.
I was thinking it was nice is SQL server allowed a 'permanent' alias for tables and columns but I don't think this feature exists.
Or I was wondering if there was a way in EF to have two names for the same property?
For the tables, you could rename them and then create a synonym with the old name pointing to the new name.
For the columns, changing their name will break your application.You could create computed columns with the old name as well, that simply display the value of the new named column though (but this seems a little silly).
Note, however, that a computed column cannot reference another computed column, so you would have to duplicate the column in its entirety. That could lead to problems down the line if you don't update the definition of both columns.
A view containing a simple select statement acts exactly like a table. You really need to fix this properly across the database and applications. However if you want to go the view route, I suggest you do this:
Say you have a table called MyTable that you rename TheTable and with a column called MyColumn that you want to rename to TheColumn
Create a schema, say, new
Move the original table into it with this ALTER SCHEMA new TRANSFER MyTable
Rename the table and column.
Now you have a table called new.TheTable with a column called TheColumn. Everything is broken
Lastly, create a view that looks just like the old table
CREATE VIEW dbo.MyTable
AS
SELECT Column1, Column2, Column3, TheColumn As MyColumn
FROM new.TheTable;
Now everything works again.
All your fixed 'new' tables are in the new schema
However now everything is extra complicated
This is basically an illustration that you should just fix it properly across the whole app one at a time with careful change management. Definitely don't complicate it with triggers
Since you are using code first with multiple web and desktop applications, you are likely managing database changes from one place through migrations and ignoring changes other places.
You can create an empty migration and add code that will change the table name and column names to what you want. The migration should then create a view that will select from that table with the original table and column names. When you apply this migration, everything should still be working as normal from all applications. There are no model changes since you didn’t touch the model classes. Inserts, updates, and deletes will still happen through the view. There is no need for potentially buggy triggers or synonyms on the table in this option.
Now that you have the table changed, you can focus on the application code. If it helps, you can add annotations over the column and table names and start refactoring the code. You need to make sure you don’t make model changes that will break the other apps. If apps ignore model changes, you can get away with adding annotations over the columns and classes on all the apps before refactoring. You can get rid of the view sooner this way.

in Django, cannot delete object because view is not updatable

I have a model called Fiche which contains charfields and a couple of foreign keys. When I try to delete one row:
f = Fiche.objects.all()[0]
f.delete()
I get the following error:
('42000', "[42000] [FreeTDS][SQL Server]View or function 'entites_pilefusion_bFiche' is not updatable because the modification affects multiple base tables. (4405) (SQLExecDirectW)")
That view is partially based on Fiche okay but I should be able to delete one row of Fiche, no? Actually, when I try to delete the same row directly in SQL Server, I have no problem.
The problem is new, it seemed to appear after I add a new field in Fiche.
I have no idea how to solve or even trace the problem. I'm completely lost , any help would be much appreciated.
Patrick
EDIT: I solved the problem by adding in the model definition of PileFusionFiche (that is based on my view entites_pilefusion_bFiche) the clause on_delete=models.SET_NULL to avoid the ON DELETE CASCADE behavior on my view.
class PileFusionFiche(models.Model):
entite = models.ForeignKey('entites.Entite')
fiche = models.ForeignKey('docentites.Fiche',blank=True,null=True,on_delete=models.SET_NULL)
class Meta:
db_table = 'entites_pilefusion_bFiche'
managed = False
Thanks again #Bistabil.
I'm guessing your problem appeared after you added a field from another table.
https://msdn.microsoft.com/en-us/library/ms187956.aspx
Paragraph about updatable views is what interests you.
You need a INSTEAD OF trigger to be able to affect multiple tables with a view.

Diagram of model not being updated when model updated from DB after table removal

I noticed that when I create a table in the DB and update the model, a new object corresponding to said table appears automagically in my diagram. However, when I remove a table and perform the same operation, the diagram stays unchanged.
I'm curious if there's a way to automate the process so the updated diagram is displayed. Is it intended behavior, by the way? What'd that be good for?

How do I automatically populate a new table in MS-Access with info from an existing table?

I am very new to MS Access and I am struggling with some things that seem like they should be the most basic. I have imported a table of data from Excel and have defined the data types for the fields. I have no problem there, but now I want to make a new table that has as a primary key one of the fields from the imported table. It looks like I can manually create this table, set the relationship, and then go back and type in each record associated with the new primary key, but this seems completely ridiculous. Surely there must be a way to automatically create one record for each unique instance in the matching field from the original table. Yet, I've scrolled through hundreds of pages of Access tutorials and Googled the question and found no satisfactory guidance.
Do I completely misunderstand what Access is all about? How do I create a new table with entries from a field on an existing table? What am I missing?
You don't specify which version of Access you are using, the suggestions listed below apply to 2010, but should be similar is other versions.
You can create new tables from existing tables using either a 'Make Table' option after selecting 'Create' -> 'Query Design', or you can manually create your table first, then use an 'Append' query.
Without knowing the design of your table it's hard to get more descriptive.
Are you populating your new table's primary key ahead of time, or relying on Auto Number to do it (preferred method)?

TPT entities derived from TPH base classes in Entity Framework 6?

I have a project that is using EF6 Database first mapped to a SQL database. This is all new so I control the EF model as well as the database schema.
I currently have a table that I'll call Vehicle for simplicity. I use a discriminator column to get subclass Entities Car and Truck. This all works fine.
Now I need to do a 'soft delete' and move any deleted vehicles to a VehicleHistory table. (After trying this w/ EF i will probably use a SQL transaction). This needs to be reviewable so I need this history table mapped as well, but I would like to keep it within the inheritance hierarchy so its easily reused in other classes.
My idea was to create 'vehiclecurrent' and 'vehiclehistory' tables with FK's to Vehicle for shared columns. i would then use TPT in EF to get 'carcurrent','carhistory', ect... derived from my TPH classes(so e.g. carhistory->car->vehicle). This is not working and I get Error 3034: "Entities w/ different keys are mapped to the same row"
So my question is basically how can I pull this off? Will this approach work and how, or is there another way to accomplish this? Thanks!

Resources