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 3633748
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:41:31+00:00 2026-05-19T00:41:31+00:00

I’ve been trying to get EF4 CTP5 to play nice with an existing database,

  • 0

I’ve been trying to get EF4 CTP5 to play nice with an existing database, but struggling with some basic mapping issues.

I have two model classes so far:

public class Job
{
    [Key, Column(Order = 0)]
    public int JobNumber { get; set; }
    [Key, Column(Order = 1)]
    public int VersionNumber { get; set; }

    public virtual User OwnedBy { get; set; }
}

and

[Table("Usernames")]
public class User
{
    [Key]
    public string Username { get; set; }

    public string EmailAddress { get; set; }

    public bool IsAdministrator { get; set; }
}

And I have my DbContext class exposing those as IDbSet

I can query my users, but as soon as I added the OwnedBy field to the Job class I began getting this error in all my tests for the Jobs:

Invalid column name ‘UserUsername’.

I want this to behave like NHibernate’s many-to-one, whereas I think EF4 is treating it as a complex type. How should this be done?

  • 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-19T00:41:32+00:00Added an answer on May 19, 2026 at 12:41 am

    UserUsername is the default name that EF Code First chooses for the foreign key in the Jobs table. Obviously, it’s not the same name that your existing database has for the FK column in Jobs table. You need to override this conventions and change the FK’s default name so that it matches your database. Here is how it’s done with fluent API:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Job>()
                    .HasOptional(j => j.OwnedBy)
                    .WithMany()
                    .IsIndependent()
                    .Map(m => m.MapKey(u => u.Username, "TheFKNameInYourDB"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I need to clean up various Word 'smart' characters in user input, including but

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.