Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6471991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:17:22+00:00 2026-05-25T06:17:22+00:00

Given this: create table Location( LocationId int identity(1,1) not null primary key, Address nvarchar(max)

  • 0

Given this:

create table Location(
LocationId int identity(1,1) not null primary key,
Address nvarchar(max) not null,
City nvarchar(max) null,
State nvarchar(max) not null,
ZipCode nvarchar(max) not null
);



create table Park(
ParkId int not null primary key references Location(LocationId),
Name nvarchar(max) not null
);

I tried this mapping:

modelBuilder.Entity<Location>();
modelBuilder.Entity<Park>().ToTable("Park");
modelBuilder.Entity<Park>().Property(x => x.LocationId).HasColumnName("ParkId");

Unfortunately that didn’t work.

using (var db = new Ef())
{
    var park = new Park { Name = "11th Street Park", Address = "801 11th Street", City = "Aledo", State = "TX", ZipCode = "76106" };
    db.Set<Location>().Add(park);

    db.SaveChanges();
}

It has this error:

The property ‘LocationId’ is not a declared property on type ‘Park’.
Verify that the property has not been explicitly excluded from the
model by using the Ignore method or NotMappedAttribute data
annotation. Make sure that it is a valid primitive property.

How should I map Park entity so its LocationId property fall to ParkId column?

I have this mapping by the way:

public class Location
{
    public virtual int LocationId { get; set; }
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual string ZipCode { get; set; }
}

public class Park : Location
{
    public virtual string Name { get; set; }
}

If it could help, this is possible in EF 4.0 (via designer), just followed the steps in Chapter 2-11 of Entity Framework 4.0 Recipes, Problem Solution Approach. Now I’m trying it on code first via EF 4.1

[EDIT]

If I change the ParkId to LocationId, things are ok. However, with designer approach, it is possible to map the LocationId to ParkId of table Park; I want to achieve the same thing with code first

create table Park( 
LocationId int not null primary key references Location(LocationId), 
Name nvarchar(max) not null 
); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:17:22+00:00Added an answer on May 25, 2026 at 6:17 am

    As I know (and I tried it multiple times) code first doesn’t support this => your derived type should use same column names for primary key.

    This problem can be described very simply: Current fluent mapping implementation doesn’t allow overriding mapping rules from parent entity => parent entity defines names of primary key columns in all derived entities.

    IMO the most probable reason is that it was really designed as code first where you don’t have existing database and you do not have to bother with database naming – it was up to EF to define names as it needed. Once DbContext API was released people started to use it with existing database massively. But here comes a problem: Initial use cases didn’t count with this so some scenarios which are pretty easily done in EDMX are not possible. This is one of them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given this table definition create table herb.app (appId int identity primary key , application
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
Given the following (heavily simplified) tables: create table Tags ( TagId int Primary Key
Given this table design in a web application: CREATE TABLE `invoice` ( `invoice_nr` int(10)
Given a table holding edges in a directed graph like this: CREATE TABLE edges
Why does SQL server express 2008 give me this error? CREATE TABLE model (
I want to use this for an object factory: Given a string, create a
Suppose a tree structure is implemented in SQL like this: CREATE TABLE nodes (
I just read this link http://msdn.microsoft.com/en-us/library/ms186243.aspx Here is the code to create table and
I've a vote system which is designed like this: CREATE TABLE `vote` ( `id`

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.