How to modify an enumeration created with DB-first Entity Framework - c#

I followed the directions as detailed in this answer, and my enumeration was saved and can be utilized throughout the app. But in the case that I wanted to add additional options or change the names of existing options, how do I that?
I know I can do so by editing the EDMX file by hand in a text editor. I'd rather do it within Visual Studio in a fashion similar to how I generated the enumeration to begin with if it's possible.

You can use the entity data model browser to update this. In your menu bar, navigate to View -> Other Windows -> Entity Data Model Browser. I think you need to have your edmx file open to see this option. That opens a model browser window and under the "Enum Types" folder you can double click your enum and update it.

Related

How to View .cshtml Design Locally

I have a few .cshtml files which contains the design of a form in Visual Studio.
As I am debugging it locally, I do not have access to the database. Therefore, when I click on View in Browser, I will get errors.
May I know are there any others ways that can I preview the design of the form?
Thank you.
Visual Studio doesn't have a visual designer for razor pages like the one available for Web Forms. You could try opening the file with the "HTML (Web Forms) Editor" by using the option "Open with..." in that context menu, but I don't think it will render anything useful.
Your default option would be "View Markup", and you must interpret the layout of your form by looking at the HTML elements composing it.
I have two possible suggestions:
Create a local database with the same basic schema and change the connection to it for debugging. This requires minimal changes to the existing code and is my preferred method.
Alternatively, create some dummy data to display on the page in place of what would be filled from the database. Probably a slightly quicker solution than the first one, but potentially means a lot of code edits.

Entity Framework Error 3004 while trying to add a field into a table

this question is about adding a new field called "Position" of type integer into an existing table via the Entity Framework designer. What I expect to happen (correct me if I am wrong) is add the field, select to update the database via the model and get the new field created in my database. Instead of this, I get an "Error 3004 Problem in mapping fragments at line xxx". My environment is using Visual Studio 2015 with EF5, and the database is hosted on Oracle MySQL. The project I am working on is a website which was given to me to maintain and upgrade further.
Here are my steps:
1. Discover the table in the model browser, click Show in Diagram.
2. Inside the model I right-click on the table -> Add New -> New Scalar Property and type the new field "Position" inside the table.
3. Click save icon to save the model
4. Then I get an Error 3004:
5. I realise that in the mapping details my new field isn't showing and I can't type in there to add it manually.
6. I can see the new field has been added to the syncitem class in fact I want this to be an integer so I go ahead and change it by hand.
Tried to click save again and re-compile but I still get the same error. I need to say at this point that I have spent an entire day searching around on the web about various error codes I got other than just 3004 but here is where I stopped and turned to SO for guidance. I know there are other threads here about Error 3004 and I did read quite a few them. One of the pieces of advice I found was to change the DDL generation template to one that supports MySQL since Visual Studio will default to SQL (this is done by clicking on the white space inside the designer and then clicking the Properties tab):
I tried that as well but nothing changed, the error persists... I also tried deleting the entire table in the designer and updating from the database, the table re-appeared but with a torrent of "Error 11007" lines, plus the table's associations are not wired correctly. Another thing I did was to open the .edmx file in Notepad++ and see if I can add my new field in there, but it just felt wrong doing it like that so I quit.
Am I approaching this in the wrong way? All I want to do is add a new field! I have come to a stop, my experience with EF is not great, I felt lots of frustration with this database's particular setup, quite frankly I dare say that I think it is somewhat broken. It's like dealing with a brand new religion whenever I have to touch it!
In the long run I want to upgrade to EF6 and get rid of the designer but that's another story.
Any advice on my problem will be greatly appreciated.
Thanks.
Eventually I had to tamper with the XML file of the model (the .edmx file) to manually add in my new field and set it to integer, as there was nothing I could do to satisfy Visual Studio. The web project I am working on looks rather complicated with loads of sub-projects in it like "DataAccess", "EntityModel", "ManagerLayer", my web folders and a host of other directories. I think my EF needs a complete re-do, my guess is that it started with a very old EF version and is shuffling its feet right now in terms of maintainance.

How do I make changes to my local db in asp.net MVC 5

I am using VS2013 and .net framework 4.5. I was following this getting started tutorial when halfway down the page I noticed that there was a Ratings column that was not part of the original build and also was not explained as far as how to include the missing column into the local database and update the model. In the comments the author admits to the issue and says that he will update it, however the tutorial is 2 years old.
So, I did some digging on the matter and according to this solution I should make a change and simply click F5 but this does not work.
I even added the ratings object to the model class.
Instead of changes being saved to the project Visual Studio wants to save the changes as a separate SQL file. Should I not expect the changes to be saved to the movies.mdf? How is this done?
You are using Entity Framework Code-First according to my understanding and links you shared.
There is no need to handle the database with queries. All you need to do is "Play with the Code in Class".
Dont forget to Enable Migration to project file.
After that whenever you change the code and update your model class. Go to the Package-Manager Console and apply "add-migration migrationName" to the selected project and then apply another command "update-database".
This will update the database.

Entity Framework 6, Database First. Update Model not working correctly

My EF6 code does not reflect changes in my database, and has discrepencies within the EF6 code itself.
The class code Has the UserId property at the bottom - just under the Highlighted LoggedOnUserName property. This is correct.
However, in Visual Studio when expanding the Model.tt I see this:
As you can see the model stops at the LoggedInUserName property and does not include the UserId property.
Finally with the source code itself I can not access the Client class at all.
Sorry for all the images but it is the best I can do with the current Dev Machine.
The Question: How can I get the EF6 generated code to correctly reflect the Database? I have clicked on the designer model and updated but to no effect.
Ok this is the solution I found.
I tried using edmgen.exe which comes with EF. I opened a cmd.exe window in the solution and ran edmgen. which is a little difficult. the most awkward setting is actually the connection string to use. You can get that from web or app.cfg.
It unfortunately did not work.
What did work was to right click on the Models.edmx entry in the Solution Explorer and deleting it and then Add New Item -> Data -> ADO.NET Entity Data Model
This recreated the who thing from scratch which fixed all the problems.

C# where does the dbml file come from?

I am currently learning C# and LINQ. I have lots of questions about them. Basically, I need a step by step tutorial.
I suppose the dbml file is the configuration file of the database. If I double click the dbml file VS will open it in a design diagram. Can I create/delete/modify tables here? I can use add new item to add the LINQ to SQL Classes to get a dbml file?
What's next? Generate tables in database? Generate SQL script? Generate cs files? When? How?
The DBML file is not related to the database server at all. It's a completely client side thing. It's essentially a set of information about your tables in the database and how you're going to map them to .NET objects.
Of course, you can drag a table from a database and have Visual Studio infer some information for you automatically, but changing the file will not affect the database. You can create a DBML file from scratch without any database too.
Internally, the DBML file is simply an XML file that's fed into a custom tool by Visual Studio and generates .cs files representing the LINQ object model for your database from it.
The DBML file is mapping that defines your classes based on your database schema. Yes, it defines your (default) connection string, but it doesn't "configure" your database at all.
Linq to Sql uses a database-first approach where you have the database and model your classes after the DB schema. By dragging & dropping the table onto there, you'll be automating the creation of the classes so you don't have to type them out. You can change property names etc from there and the mapping between the property and its correct database column name will remain intact.
this link learn how to create dbml File in your Project :
http://dotnetlearners.com/linq/linq-to-sql-creating-dbml-file
Implement LINQ to SQL first we have to create the DBML file, the DBML file contains the C# source code which allow us to write the LINQ Queries to SQL. Here is the step by step process to generate dbml file.
Right click on the project (or project folder) and select the option Add New Item.
add-new-item
Select LINQ to SQL Classes Template and Give Name as MyDB.dbml.
linq-to-sql-classess
Click on Yes when the below confirmation displayed.
dialog
MyDB.dbml file will be added under App_Code folder.
dbml
Expand Server Explorer and Right click on the Data Connections and select the option Add Connection.
add-connection
Add Connection Pop up will be opened, give the SQL Server details and select the then click on OK button.
server-detail
Database will be added under Data Connections as shown below.
server-explorer
Drag the table in the left pane and if primary key & foreign key relations are there then it will automatically show as shown below.
drag-tables
Drag the stored procedures to the right pane.
drag-store-procedures
Related C# code will be automatically generated and we can see by opening the file MyDB.designer.cs.
dbml-designer-cs
Yes, the DBML file is created when you add a Linq to SQL class. In the designer (what you see when you double click the DBML file) you can drag tables (from the server explorer) onto it. You can then reference these tables in your code. There are more than a few getting started tutorials out there:
Check this SO question for details:
https://stackoverflow.com/questions/481244/can-anyone-recommend-a-good-tutorial-for-learning-linq2sql
Walkthrough: Simple Object Model and Query (C#)
Consider SqlMetal
PK :-)

Categories