Updating EF6 edmx model causes new model file to be created - c#

I'm using EF 6.1 with the downloaded T4 template from VS. When I try to update the EF model with new information, or just issue a refresh from my database, the model file duplicates and I get a massive amount of errors.
So, for example, my model file is called Test.edmx, which has subfiles called Test.Designer.vb and TestModel.tt. The TestModel.tt has a subfile called TestModel.vb which appears to contain all my entity framework models.
When I right click on the design surface and select "update model from database", and either add a new entity or refresh an existing one, instead of updating the TestModel.vb file, it creates a whole new file called TestModel1.vb. Once that happens I get thousands of duplicate item exceptions, because now there a doubles.
Why is this happening, and how can I fix it? Is something configured wrong? This model was previously using EF4 and was upgraded straight to 6 using these instructions. https://msdn.microsoft.com/en-us/data/upgradeef6.aspx

It sounds like the linkage between your model/tt file and the output file has been broken. You could open the project (vbproj) file in a text editor and look for test.edmx or testmodel.tt. You should find one or both of them inside nodes somewhere. The file in the enclosing node is the one that depends upon them. At a guess you'll find this is currently TestModel1.vb. You could try editing this and reopening the project, but the simplest thing might be to exclude your existing TestModel.vb and let visual studio have its way. It should be a hidden file after all

Related

Database seems to update during execution, then changes disappear

I'm trying to create a recipe book in C# where I can easily search for what recipes I can make with what ingredients are on hand. This is mostly a learning exercise for me at this point.
Right now, I'm trying to test updating the tables in my database. It seems to work during one instance of the code, but the changes are gone the next time I run it.
So I have a table in my database like this:
I create a small list of example recipes from a json file
var js = new JsonImporter() { SourceDirectory = Path.Combine(Directory.GetCurrentDirectory(), "test_data") };
List<Recipe> recipes = js.ImportData<Recipe>("test_recipes.json");
Connect to my table and InsertAllOnSubmit:
DataContext db = new DataContext(connectionString);
Table<Recipe> db_recipes = db.GetTable<Recipe>();
db_recipes.InsertAllOnSubmit(recipes);
Show how many entries are in the table, submit the changes, refresh the number of entries, and show how many are in the table again:
MessageBox.Show(db_recipes.Count().ToString());
db.SubmitChanges();
db_recipes = db.GetTable<Recipe>();
MessageBox.Show(db_recipes.Count().ToString());
The first messagebox shows 1 (I have one row in the table that I entered manually via the Server Explorer), and the second message says 3. This is what I expect, since I am adding 2 entries.
However, once I run the code again, the message boxes again say 1 and 3, instead of always incrementing by 2. So it seems to me like the database is not saving. Can anybody help me figure out what's going on here?
You never said what database you use but I think it highly likely to be file based, either Access or SQL Server (in "attach the mdf file at runtime" mode)
Often with file based databases we end up setting them up so that there is a version of the db file in the project folder alongside the source code. This is copied to the output directory every time the project is built and the output directory contains the exe and the db. The exe modifies the db in the output folder. The next time it is built / run the blank db from the source code folder is copied over the top of the db that was modified in the output folder
Run your project, get the exe to modify the db, stop the exe and then search the whole project folder (using windows file search) or even the whole hard disk for the name of your db file. You'll figure out what's going on by looking at the file modified dates..
If it is a file based db thing, find the db in CS Solution explorer, click it and set its "copy to output" setting to "copy if newer" so that it only wipes the output db if eg the scheme has changed

visual studio randomly updates designer.cs file for my dbml

I have a linq-to-sql connection for my database. I added a new table to the db in SQL server management studio today. Since it doesn't get automatically added in the dbml file, I dragged and dropped the table from server explorer as visual studio suggests.
This automatically makes the necessary changes to its designer.cs file, which is good.
Unfortunately, it also makes some other changes I didn't ask for. There are a bunch of variables getting renamed in the designer file consistently, which has nothing to do with the table I'm adding or the one it's connected to!
I am being very careful not to move any other table in that layout- I'm just doing a drag 'n drop in an empty area. I tried this multiple times with the same result, so maybe I'm doing something wrong. I searched everywhere and I can't seem to find a better way to update the dbml file for tables added ( I found a bunch of questions about updating the dbml file when columns are added to existing tables, unfortunately those answers don't apply here).
Has anyone ran into this issue before? Is there any solution to this?
The solution is to ignore it. The entire purpose of designer files is that they are tool-generated and may be regenerated at any time, for any reason, with any number of changes.
Under no circumstances should you modify them, and there's usually not much of a reason to look at them.
I had to fix it the hard way- manually updating the variable names by looking at the diff of the designer file. It was not fun, but it got rid of all the errors.
Apparently, it was renaming a bunch of associations for some other table consistently. Turns out someone had renamed those associations in the designer file instead of the .dbml file. So every time any change is made to the .dbml file, the designer file recompiles, and old names come back! I should probably update those associations in the dbml file so we don't have this problem in future.

Why is my edmx file not updating

I have a database first ASP.NET MVC 5 EF6 project. I'm using VS2015 CE. When I change my database (SQL Server 2012), I then go to VS to update my edmx file like this:
Double click on the edmx file in the solution explorer
In the edmx file right click on a blank space and select the option 'Update Model From Database'
I click on the button 'Finish'
I Rebuild the solution
When I have a table with the columns ID, Column1, Column2 and I changed Column2 into Column2Changed, my edmx has a model with ID, Column1, Column2, Column2Changed instead of ID, Column1, Column2Changed.
Also, when I change the datatype for a column, this change is not applied in my edmx.
This is a well-known problem with Entity Framework.
"Update Model from Database" continues to be a hit-and-miss affair. Sometimes it works, often it doesn't.
Personally, I just delete the "updated" table from my .edmx file, then re-add it. Then it seems to be work okay.
This worked for me :
1.Build the project after updating EDMX file.
2.Right click your .tt file in solution explorer.
3.Select "Run Custom Tool" option.
This will update the .tt file.
Make sure that you selecting the objects from Update tab
When faced with this issue, i solved my by change the username and password...so i would say, make sure you have a valid connection string in your edmx
I've had this issue on a number of occasions. For simple problems I've edited the edmx file by opening in the code editor ( Open With... > Source Code (Text) Editor ).
Locate the table by looking for by searching for EntityType Name="your table name".
Typically I see an entry that doesn't look right, delete it, save and close the file. Open the edmx the usual way and refresh the model and Presto problem solved.
After updating or refreshing and not getting expected results.
I worked as follows:
1. I added procedure without importing it.
2. I opened edmx with text editor and added my return types having complex type like other already present in it.
3. added all values like other procedures had.
That solved my problem!
Maybe it's not logical but when We add empty table to Edmx, it is not added. If Adding a table with some records, will fix the problem.
Fix: Allways when an error occurred, when you open edmx in notepad, errors shows, maybe you dont have primary key and so on.
What worked for me:
I updated the edmx model and then clicked Save button in edmx designer.
It seems like the code for all modified entities (views, tables, etc) is regenerated only on Save.
I faced the same issue. The most reasonable solution could be to delete .edmx file and to regenrate it. Keep in mind that you should always try to update model first if nothing work then do what I just said.

Losing database update after changes in code

I have a simple question. I'm new to .net and sql and I'm trying to write a small form application. I added a data source which I created in sql. I got textboxes, button to update tables in database. When I made changes in database, like adding or deleting rows from tables, these updates are getting lost if I made a little change in application code (for example entering a new line).
Probably I'm unaware of something simple. What is the cause of it?
Select the database from solution explorer, go to properties (or alt + enter) and change the Copy to Output Directory to Copy if newer (copy if you change the database) or Do not copy. If the selected option is Copy always, the database is copied each time the project is build.

Drag and drop stored procedure on .dbml file(VS 2010) and upon saving it creates another designer1.cs file

In my project, I have a data-access layer that contains .dbml file(named test.dbml) used to drag and drop stored procedures and tables. Two days ago when I drag and drop the stored procedure onto my test.dbml file. Upon saving it creates another test.designer1.cs class and the old test.designer.cs class remains unchanged. I use visual studio 2010 and it was very weird for me. I've checked my rights on the folder which contains my project but all seems ok because two days earlier it was working fine and suddenly it happened. I Google it but find no solution so I decided to write this question.
At-last i've solved it myself.........and i am going to share it that what i have done. my dbml file name was test.dbml having test.dbml.layout and test.designer.cs files now the problem was when ever i drag and drop stored procedure or table it creates a new designer file with name test.desginer1.cs and when i build data-ccess layer it gives me hell of errors my brain was out at that time and with angry face i delete the test.designer.cs (original file) from the solution after that there was only one file test.designer1.cs which i renamed from test.designer1.cs to test.designer.cs and build data-access layer and i was wondered that it was build successfully. this solve my problem however there are other points as well to keep in mind
make sure your dbml files should not be read-only
you have full access to write file on disk (means check your rights on directory where your solution is placed)

Categories