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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:47:36+00:00 2026-06-08T11:47:36+00:00

I have a model as shown below (created using Model-First approach). A book is

  • 0

I have a model as shown below (created using Model-First approach).

A book is a selling item. A DigitalDisc is a selling item. This is working fine and data is getting inserted correctly in database.

I am adding a new entity named “Purchase”. One purchase contains multiple sellingitems. Once I added this new association and updated the database schema, I am getting the following exception.

An error occurred while updating the entries.

“The INSERT statement conflicted with the FOREIGN KEY constraint \”FK_PurchaseSellingItem\”. The conflict occurred in database \”LibraryReservationSystem\”, table \”dbo.Purchases\”, column ‘PurchaseId’.\r\nThe statement has been terminated.”}

How to overcome this?

Note: I need to create a book (like entering book data into database). The purchase may or may not happen for this book. When the book is created the foreign key column should be null for that book. But when that column is updated, it should be an ID present in Purchase table Is that possible?

enter image description here

Note: When I try to make the field “PurchasePurchaseId” nullable in model, I am getting following error:

Error 1 Error 113: Multiplicity is not valid in Role ‘Purchase’ in relationship ‘PurchaseSellingItem’. Because all the properties in the Dependent Role are nullable, multiplicity of the Principal Role must be ‘0..1’. C:\Documents and Settings\U16990\My Documents\Visual Studio 2010\Projects\LijosEF\LijosEF\MyModelFirstTest.edmx 35 3 LijosEF

CODE

class Program
{

    static string connectionStringVal;

    static void Main(string[] args)
    {

        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        sqlBuilder.DataSource = ".";
        sqlBuilder.InitialCatalog = "LibraryReservationSystem";
        sqlBuilder.IntegratedSecurity = true;

        // Initialize the EntityConnectionStringBuilder.
        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
        entityBuilder.Metadata = @"res://*/MyModelFirstTest.csdl|res://*/MyModelFirstTest.ssdl|res://*/MyModelFirstTest.msl";

        connectionStringVal = entityBuilder.ToString();


        AddBook();
        AddDisc();





    }

    private static void AddBook()
    {

        //string connectionstring = "Data Source=.;Initial Catalog=SalesDB;Integrated Security=True;Connect Timeout=30";
        using (var db = new MyModelFirstTestContainer(connectionStringVal))
        {
            Book book = new Book();
            book.AvailabilityStatus = "Available";
            book.Price = 150;
            book.Title = "Maths Easy";

            db.SellingItems.AddObject(book);
            db.SaveChanges();

        }

    }

    private static void AddDisc()
    {

        //string connectionstring = "Data Source=.;Initial Catalog=SalesDB;Integrated Security=True;Connect Timeout=30";
        using (var db = new MyModelFirstTestContainer(connectionStringVal))
        {
            DigitalDisc disc = new DigitalDisc();
            disc.AvailabilityStatus = "Available";
            disc.Price = 300;
            disc.Artist = "Turtle Violin";

            db.SellingItems.AddObject(disc);
            db.SaveChanges();

        }

    }

}

REFERENCE:

  1. Database design – articles, blog posts, photos, stories

  2. Can I get Entity Framework (model-first) to generate composite keys?

  • 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-08T11:47:38+00:00Added an answer on June 8, 2026 at 11:47 am

    You configured your relation to be mandatory but you are saving SellingItem without related Purchase. I would expect that EF will throw some exception before saving records to the database but it didn’t and it most probably tries to pass default value for FK column and you don’t have any Purchase with such Id.

    Btw. shouldn’t you use many-to-many relation instead because selling item can be sold multiple times and purchase can have multiple selling items?

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

Sidebar

Related Questions

I'm using jqgrid with MVC 3. I have this pages whose code is shown
Form contains two dropdown lists created using code below. Both dropdown list elements have
I've created a registration system using django-registration. I have extended the RegistrationFormTermsOfService as shown
I have a model class that uses a custom validator as shown in following
Let's say I have a QTableView with a corresponding model. This view shows numbers
I have created an RDF/OWL file using Protege 4.1 alpha. I also created a
I have a simple 'Service' system set up with an interface as shown below.
I think Im taking the wrong approach to this and have tried to find
I have an ASP.NET application that is using the SQLServer Session mode. I Created
I am using MVC. I have a view model which contains a Business object.

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.