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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:14:33+00:00 2026-05-27T10:14:33+00:00

Given the following simplified model: public class Account { public Account() { Id =

  • 0

Given the following simplified model:

public class Account
{
    public Account()
    {
        Id = Guid.NewGuid();
        ContactCard = new ContactCard();
    }

    //[ForeignKey("ContactCard")]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Number { get; set; }
    public ContactCard ContactCard { get; set; }
}

public class ContactCard
{
    public ContactCard()
    {
        Id = Guid.NewGuid();
    }

    public Guid Id { get; set; }
    public Account Account { get; set; }
}

public class MightDbContext: DbContext
{
    public DbSet<Account> Accounts { get; set; }
    public DbSet<ContactCard> ContactCards { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Account>().HasRequired(x => x.ContactCard).WithOptional(x => x.Account);
    }
}

public class MightDbInitializer : DropCreateDatabaseIfModelChanges<MightDbContext>
{
    protected override void Seed(MightDbContext context)
    {
        var accounts = new List<Account> 
        {
            new Account()
            {
                Name = "Acme Corporation Pty. Ltd.",
                Number = "001ABC"
            },

            new Account()
            {
                Name = "Three Little Pigs Pty. Ltd.",
                Number = "002DEF"
            }
        };

        accounts.ForEach(c => context.Accounts.Add(c));
    }
}

And the following simple console program to iterate the contents of the Accounts and ContactCards collections:

static void Main(string[] args)
    {
        Database.SetInitializer<MightDbContext>(new MightDbInitializer());

        using (var context = new MightDbContext())
        {
            foreach (Account c in context.Accounts)
            {
                Console.WriteLine(c.ContactCard.Id);
            }

            var contactCards = context.ContactCards.ToList();  /* ERROR OCCURS HERE */

            foreach (ContactCard a in contactCards)
            {
                Console.WriteLine(a.Id);
            }
        }

        Console.Read();
    }

Why do I get the following error as soon as I try to access the ContactCards collection:

Multiplicity constraint violated. The role ‘Account_ContactCard_Source’ of the relationship ‘InvestAdmin.Might.DataAccess.Account_ContactCard’ has multiplicity 1 or 0..1.

When I look at the data that has actually been stored in the database tables all seems to be correct. In fact here is that data:

Accounts:
Id  Name    Number
ab711bad-1b32-42ca-b68b-12f7be831bd8    Acme Corporation Pty. Ltd.  001ABC
dc20a1dd-0ed4-461d-bc9c-04a85b555740    Three Little Pigs Pty. Ltd. 002DEF

ContactCards:
Id
dc20a1dd-0ed4-461d-bc9c-04a85b555740
ab711bad-1b32-42ca-b68b-12f7be831bd8

And for completeness here is the Account_ContactCard foreign key constraint as defined in the database:

-- Script Date: 06/12/2011 7:00 AM  - Generated by ExportSqlCe version 3.5.1.7
   ALTER TABLE [Accounts] ADD CONSTRAINT [Account_ContactCard] FOREIGN KEY ([Id]) REFERENCES [ContactCards]([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION;

I have been reading all I can on defining one to one relationships in Code First and have tried many different configurations. All end up back at the same problem.

  • 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-27T10:14:34+00:00Added an answer on May 27, 2026 at 10:14 am

    I found that I could resolve this problem by moving the creation of the associated ContactCard from the Account constructor into the actual creation of the Account objects in the Seed method of the DBInitializer. So it appears that it was not (directly) a problem with the One to One relationship.

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

Sidebar

Related Questions

Given the following (simplified) classes: public class Master { public int Id { get;
I have the following (simplified) classes: public class Person { public string Id {get;
Given the following simplified code block in ASP.NET <% foreach( item in Model) {
Given the following POCO Code First Entities public class Customer { public int CustomerId
I am using the SQLite database and have the following persistent class (simplified): public
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Given the following code: final class retVal { int photo_id; } Gson gson =
Given the following models: team.rb class Team < ActiveRecord::Base has_many :events, :dependent => :destroy
Given the following (heavily simplified) tables: create table Tags ( TagId int Primary Key
Given the following table (much simplified for the purposes of this question): id perPeriod

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.