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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:34:59+00:00 2026-06-14T10:34:59+00:00

Using EF-Code-First I want to set up a DB table that is related (1-1)

  • 0

Using EF-Code-First I want to set up a DB table that is related (1-1) to the inbuilt Simple Membership provider that comes OOTB with ASP.Net 4.0+

The UserProfile class in AccountModels.cs is a simple 2 field class:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
}

My class with other user info I want to keep separate has this format:

[Table("UserInfo")]
public class UserInfo
{
    [Key]
    public int ID { get; set; }
    <user info fields here...>
    public virtual UserProfile UserProfile { get; set; }
}

The Context is:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("DefaultConnection")
    {
    }
    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<UserInfo> UsersInfo { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserInfo>()
                    .HasRequired(u => u.UserProfile)
                    .WithRequiredPrincipal();
    }
}

Now, without changing anything else, when I run the app, the models build fine, but I’m getting a error when WebSecurity.CreateUserAndAccount(model.UserName, model.Password); runs (Acct Controller Line 82). The INSERT statement conflicted with the FOREIGN KEY constraint "UserInfo_UserProfile".

What is the (assumed) preferred method of storing user information that I want to be able to reference directly from the DB later (ie. not in blobs as per the old Profile provider)

How do I set this up using EF-CodeFirst as opposed to running everything through a repository layer? Should I just go the repository route?

I had trouble getting azureSQL to include more columns in the UserProfile table when I tested that method, so would like to keep the userInfo stuff out of UserProfile.

Thanks

  • 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-14T10:35:00+00:00Added an answer on June 14, 2026 at 10:35 am
    
     modelBuilder.Entity<UserInfo>()
                        .HasRequired(u => u.UserProfile)
                        .WithRequiredPrincipal();
    

    Means your UserProfile class in turn requires UserInfo. Since SimpleMembership will insert into UserProfile and not UserInfo, the exception is thrown.

    I don’t think you need this off the top of my head since your relationship is optional – UserProfile will exist before UserInfo, as simplemembership will create it prior to you creating a UserInfo.

    http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/257f33d2-9870-46ce-9b0b-147935162a0c

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

Sidebar

Related Questions

Hello everyone my problem is very simple. Using entity framework code first I want
I've tried using Entity Framework Code First and set AutomaticMigrationsEnabled to true so that
(USING ENTITY FRAMEWORK 4.3 CODE FIRST) The code is below. I want to actually
I'm using Code First with EF 5. Currently I have SPs that return just
In .NET4 MVC 4 using Code First I have two classes that form a
I'm using EF Code First (4.3.1) on a personal ASP.NET MVC 3 project, with
Using EF code first want to control the name of the genereted column WHEN
I'm using EF4.1 with Code first and TPT (Table per Type) inheritance. I have
I am using ASP.NET MVC 3 and Entity Framework 4.1 code first . I
Easier to show by example -- I'm using code-first to construct a database. I

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.