Solved

.NET MVC 5 Code First from Existing Database Primary Key Problem

Posted on 2015-01-06
1
312 Views
Last Modified: 2015-01-06
Hi Experts,

I have a .NET MVC project, and an existing database, and I'm trying to generate a code-first model from it. Several of the tables work fine, and several do not. For example, one table called "Actions", with a primary Key "ActionID" (String) generates a model that has no primary key. I have another table, called "LogEntities" with primary key "EntityID" (String), and that one detects the key just fine. I know there are conventions when identifying the key, but it seems that the tables that follow the convention fail, and the ones that don't... work properly?

I can't change the structure of the database, as it's accessed by other applications, so how can I either:

1) Force the primary keys to be detected as primary keys
2) Force the model builder to use them as keys anyway

I've tried this in the model builder (which i got from a stack overflow post, translated to VB):

modelBuilder.Properties(Of String).Where(Function(x) x.Name = "ActionID").Configure(Function(x) x.IsKey)

Open in new window


Which supposedly will force any field called "ActionID" to be used as a primary key, but that has no effect. Please help!

Thanks!
0
Comment
Question by:mylescardiff
1 Comment
 
LVL 1

Accepted Solution

by:
mylescardiff earned 0 total points
ID: 40534555
Sorry, stumbled on the answer to this 5 min after posting:

For anyone else with this problem, you need to add the following lines for each table that does not have a key defined:

 modelBuilder.Entity(Of Action).HasKey(Function(x) x.ActionID)

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now