How to update Datebase from edmx Model first? - c#

I have made a Entityframework model first to create tables to the database,
but then I found mistakes in my design, so I change the edmx model manually,
including changing the constraints between relationship as well as multiplicities.
So now, more error occur :
Error 3032: Problem in mapping fragments starting at lines ...,... are being mapped to the same rows in table Risk. Mapping conditions can be used to distinguish the rows that these types are mapped to.
Seems it's because the inconsistency between database and the model, there are lots of article discussing how to update the model from database, but not to update the database from the model. (Sorry if I missed a lot...)
To the person who's not familiar with T-SQL, the way convenient for me is just to update the model then expect the database will be updated consistently.
So please tell me is there any solution of this?

Related

EF code first created a column on DB table that does not exist in my model

I didn't find any question here that addresses this problem specifically, so here it goes.
I'm using EF Core Code First. One of the tables generated by EF migrations has an additional column that does not exist in my model. It might have existed, as I can see in the ModelSnapshot file, but it was removed a long time ago.
This entity does not have any complex object apart from the identity user and all other properties are primitive. (strings, ints, etc)
I have already tried to add that same field and generated a migration and it appears empty.
Then I tried to remove the field from the model and generated the migration again to see if now it recognizes I am removing it, but again.. migration empty.
I am thinking of removing it from the DB manually but the problem is that it still exists in the model snapshot file, which can generate other sync issues.
I would appreciate any ideas on how to solve this. Thank you very much.

Entity Framework create tables or fields if using dbfirst

The project uses dbfirst approach. On my database I create and edit tables , Then I update the models from the database (edmx).
Is there a way to solve the problem when my models are more relevant than the customer base? It is very important to solve this with the help of EF.
That is, I need to in the customer database automatically added new fields in the tables and created the tables themselves if they were not
You can use Visual Studio over your database model through the designer interface and them updated your database. However, it's a procedure i don't recomend you to do, because depending what operations you want to perform on your database after being applied can rise errors related with some specific types and other specific things and then the roolback could not be so easy.

Entity Framework migrations under the hood

I've been using Entity Framework migrations for some time and I started wondering how they actually work under the hood. I mean the following:
How does EF understand that the model in the application and the db scheme are different? As far as I know, there's a table __MigrationHistory in the database where all migrations are stored as well as in the Migrations folder in the application (applies only to code-based migrations). In the __MigrationHistory table, there's a column called model which contains some kinda hash. What is that hash exactly? Is it a snapshot of the model? Is it the changes that EF needs to apply to get from the previous migration to this one?
If it was the model snapshot, it would mean EF would have to figure out how to transform the current model to the snapshot each time we decided to update database.
However, if it was the changes, it would mean EF would have to apply those changes to the current model in order to understand when the db model and the application model are different.
The question is, where can I read something about how the migrations are implemented and what is this model column in the database. I would appreciate any advices or links.
Update:
I've checked the resources provided and as I've found out, the model column is actually the snapshot of the model. That means, when I run the update-database command, EF goes to the db, checks the latest migration model by decoding the XML string and if the current model in the application and the model EF got from the db are different, EF generates a script to update the db. However, I still don't know what EF does when there are multiple migrations pending.
I will describe on example based on the example from the https://channel9.msdn.com/Blogs/EF/Migrations-Under-the-Hood. Say, we have First migration in your db, then Second migration, which adds the Url column and the Third one, which drops this column. If I were to apply these changes to the db which contains the schema similar to First, would EF add the column and then drop it according to the Second and Third, or will it try to calculate the general changes that are required to update the db and then execute the generated script (in the example case it would do nothing?
Also, there's another link I've found if anyone's interested
https://msdn.microsoft.com/en-us/data/dn481501.aspx
This Channel 9 video covers the general concepts. This blog post might be more specific to your question. In particular, the author concludes on the use of model column (spoiler: its a compressed XML string you can decompress and inspect, has code to do so).

View using same type as Table

I have a table that used throughout an app by Entity. I have a view that returns an identical column set, but is actually a union on itself to try to work around some bad normalization (The app is large and partially out of my hands, this part is unavoidable).
Is it possible to have Entity 4 treat a view that is exactly like a table as the same type, so that I can use this view to populate a collection of the same type? This question seems to indicate it is possible in nhibernatem but I can't find anything like it for entity. It would be an extra bonus of the navigation properties could still be used to Include(), but this is not necessary (I can always manually join).
Since EF works on mappings from objects to database entities this is not directly possible. What you need is something like changing the queried database entity dynamically, and AFAIK this is not possible without manually changing the object context.
For sure the EF runtime won't care as long as it can treat the view as if it was completely separate table. The two possible challenges that I forsee are:
Tooling: Our wizard does allow you to select views when doing reverse engineering (i.e. database-first). Definitively if you can use 'code first against an existing database' you can just pretend that the view is just a table, but you won't get any help scripting the database creation or migrations.
Updates: in general you can perform updates for a view setting up store procedure mapping (which is available in the EF Designer from v1 or in Code First starting in EF6). You might also be able to make your view updatable directly or using instead off triggers (see "Updatable Views" here for more details). If I remember correctly the SQL generated by EF to retrieve database generated values (e.g. for identity columns) is not compatible in some cases with instead-off triggers. Yet another alternative is to have your application treat the view as read-only and perform all updates through the actual table, which you would map as a separate entity. Keep in in mind that in-memory entities for the view and the original table will not be kept in sync.
Hope this helps!

update conceptual model

I have generated EDM from DB. Later I have removed a column from a DB and once I have updated the model the property in entity for that column is still there. Also I have noticed that even if you will change a column type and refresh the model the Entity in model is not modified.
Is there a way to refresh EDM and apply changes done in DB?
DB changes will be pushed through to the conceptual model in most cases, removing a column is a special case. If the model refresh included this feature, then it might accidentally remove properties that you had added to the conceptual model independently - that would be a bad thing.
To remove an obsolete column, just highlight it in the designer and hit the delete key. You can also remove the column in the edmx file if you can handle editing CSDL.
When you update the EDM it only updates the database definition not the conceptual model. Probably you get errors afterwards complaining about a mapping failure. You still have a conceptual property that doesn't map to a database property anymore. Remember that the EDM has three parts in it. Only the first time the conceptual model is created based upon the database model and the two are linked together by the mapping model. Afterwards, when updating the EDM, only the database model is refreshed. You have to delete / redefine types of properties by hand.

Categories

Resources