Last post May 09, 2018 09:40 AM by DA924
Jan 31, 2017 11:11 PM|aidawn|LINK
Coding an MVC asp.net web application with an existing database (db first).
My existing database has table tbluser with a foreign key idworker refrencing table tblworker primary key id.
With the models folder selected, I add new item, Data, ADO.net Entity model, connect to my database and choose the 2 tables :
tbluser and tblworker
I make sure that Include foreign key columns in model option is selected.
But the navigation properties are not added. The model class for tblwork doesnt have a tbluser property of type tbluser.
Very new to MVC entity framework The tutorials I ran through movies and ContosoUniversity one are so cool. I have a lot of tables in this legacy database with foreign keys and would love to see foreign key columns included in my models. Any suggestion is
Feb 01, 2017 01:50 AM|ignatandrei|LINK
the navigation properties are not added. The model class for tblwork doesnt have a tbluser property of type tbluser.
Could you verify
1. Are you accessing the correct database?
2. In the database diagram (with SSMS )the relationship are there?
Feb 01, 2017 02:34 AM|aidawn|LINK
Correct database is a good question. Whenever I think I am going crazy it is usually because I am in the wrong database. I have access to a lot of them. But for this project, yes I am very certain that I am in the correct database.
In SSMS I used database diagram and see the relationship and when I hover over the connector it shows my fk
I also scripted out the foreign key on tbluser and the primary key on tblwork in case that helps.
ALTER TABLE [dbo].[tblUser] WITH CHECK ADD CONSTRAINT [fk_tblUser_idworker] FOREIGN KEY([idWorker])
REFERENCES [dbo].[tblWork] ([ID])
ALTER TABLE [dbo].[tblWork] ADD CONSTRAINT [PK_ID] PRIMARY KEY CLUSTERED ([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
Feb 01, 2017 02:46 AM|aidawn|LINK
Also I am creating my project in MSVS 2013 and NuGet Package Manager shows that EntityFramework is version 6.1.3
When I did the ContosoUniversity tutorial I used the same version with success. It must be the database. I wonder if it is the integrity of the data itself.
Feb 02, 2017 07:05 AM|AngelinaJolie|LINK
I suggest that you can rebuild and update the two models from the database .
Here are some detailed steps.
1. double-click Model1.edmx
2. delete the EF entity model of the two tables
3. Ctrl+ S,To save.
4.To find the corresponding class file in Models folder , manually delete them.
5. right-click in Model1.edmx, select Update Model from Database..
6. Ctrl+ S,To save.
Feb 02, 2017 07:24 AM|ignatandrei|LINK
yes I am very certain that I am in the correct database.
It is not enough. Please verify
1. In the web.config file in the root of the site
2. In the code - that you are accessing the correct connectionstring from the point 1.
Feb 04, 2017 05:45 PM|aidawn|LINK
1 In web.config I found the connection string to be correct and the only one. I deleted the default to localdb.
2 I had not added scaffolding to my project as of yet. Only the models from the database. In the tutorial at this point I had model classes for my tables and the relationship between the tables with foreign keys were added to the model classes. But not
for my tables in my database.
Could it be the version of SQL Server it is Express 2008R2?
Feb 04, 2017 05:46 PM|aidawn|LINK
Thank you for your reply but sadly this did not work. I think I will begin again from the beginning.
Feb 04, 2017 06:43 PM|aidawn|LINK
I have it working now. I sincerely thank you for your replies.
I opened my edmx and added the Association.
It is still a quandary, to me at least, that in the exact same environment (VS2013, EF 6.1.3, SQLServer2012) adding data model from one database provides the association automatically but not for another. Maybe the multiplicity was ambiguous and stepping
through the wizard specifying multiplicity was the issue. When I used the wizard I did have to switch multiplicity settings on each table from the wizards default.
Feb 07, 2017 02:11 AM|AngelinaJolie|LINK
I suggest that you could check the 'Include foreign key columns in the model' item whether has selected. From MSDN
And a unique, non-clustered index allows for a null value which can't be mapped. From
Please have a try.
May 08, 2018 11:53 AM|Suncat2000|LINK
Sometime Visual Studio just goes stupid. Try these steps:
These steps worked for me when Visual Studio refused to add the foreign key association. I had to do this multiple times to get all the foreign key associations restored between entities in my model.
As with most things Microsoft, rebooting often corrects a lot of misbehavior.
May 09, 2018 09:40 AM|DA924|LINK
Did you try making another project like a console application and implement EF in it pointing to the database tables to see that it works correctly in building the navigation properties?