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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:08:43+00:00 2026-06-12T21:08:43+00:00

I’m having some troubles getting a set of Entities setup to work correctly. I’m

  • 0

I’m having some troubles getting a set of Entities setup to work correctly. I’m using EF v5 in VS2012 against SQL Server 2008 R2 Express.

Everything seems to be correct with the generated database but I’m not having any success writing to some of the tables.

I have a UserProfile object defined like:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    public long UserId { get; set; }

    [StringLength(56)]
    public string UserName { get; set; }

    public Country Country { get; set; }
    ...
}

I also have the Country entity defined like:

[Table("Country")]
public class Country
{
    [Key]
    public int Id { get; set; }

    [StringLength(2)]
    public string CountryCode {  get; set; }

    [StringLength(100)]
    public string CountryName { get; set; }

    public ICollection<UserProfile> UserProfiles { get; set; }
    ... 
}

The generated columns look correct in the database, if I script it out it looks good:

ALTER TABLE [dbo].[UserProfile]  WITH CHECK ADD  CONSTRAINT [UserProfile_Country] FOREIGN KEY([Country_Id])
REFERENCES [dbo].[Country] ([Id])
GO

Just for testing purposes in a controller action I have the following:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new 
{
    Country = new Country { Id = 223, CountryCode = "UK", CountryName = "UK" },
    ...
});

When this WebSecurity method is executed I get the following error:

No mapping exists from object type MyApi.Data.Entities.Country to a known managed provider native type.

I tried setting up a configuration file to specify the relationship in code as well as the database but it’s still not playing friendly.

The config looks like:

public class CountryEntityConfig : EntityTypeConfiguration<Country>
{
    public CountryEntityConfig()
    {
        this.HasMany(x => x.UserProfiles)
            .WithRequired()
            .HasForeignKey(FKey => FKey.Country);            
    }
}

It feels really odd having a list of profiles in the country object am I getting some of the basics completely wrong with this? Any input appreciated.

EDIT:

I’ve revisted some classes and made a few changes, in the user profile I now have:

    public int CountryId { get; set; }

    public virtual Country Country { get; set; }

Country.cs

public class Country
{
    public int CountryId { get; set; }

    [StringLength(2)]
    public string CountryCode {  get; set; }

    [StringLength(100)]
    public string CountryName { get; set; }

    public virtual ICollection<UserProfile> UserProfiles { get; set; }
}

And the database now contains two CountryId related fields in the UserProfile table with relationships to the Country table and I still get the original error message.

TIA,

  • 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-06-12T21:08:44+00:00Added an answer on June 12, 2026 at 9:08 pm

    The No mapping error is probably because you did not add the Country entity to the DbContext.

    In the AccountModels.cs file, you will find a UsersContext that derives from DbContext, in there you must add a DbSet<Country>.

    There are, however, other issues here. For instance, you are creating your Country table with a Key, and EF by default will give the Country table an autogenerated identity column. This means you can’t insert a value for the ID, it will be auto generated.

    Country is probably a lookup table anyways that you will populate with country values. So you would probably just set CountryId to the value of the country you want

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.