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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:20:28+00:00 2026-06-17T08:20:28+00:00

In my web store mvc app I want to add items to database. Items

  • 0

In my “web store” mvc app I want to add items to database. Items table has CreatedBy field and it is a foreign key from User table UserId field. Everything was working before I put the database into the App_Data folder. Now I get the SqlException when trying to create a new Item:

The INSERT statement conflicted with the FOREIGN KEY constraint “FK_Item_contains_User”. The conflict occurred in database “C:\USERS\ALEKSEY\REPOS\2\WEBSTORE\WEBSTORE\APP_DATA\WEBSTORE.MDF”, table “dbo.Users”, column ‘UserId’.

Here is the Create method of ItemRepository class:

public Item CreateItem(int productId, Guid userId)
    {
        var item = new Item
            {
                ProductId = productId,
                CreatedBy = userId,
            };
        _dataContext.Items.InsertOnSubmit(item);
        _dataContext.SubmitChanges(); // in this line the exception occures !
        return item;
    }

Here is the controller method Create:

    [HttpGet]
    public ViewResult Create()
    {
        var p = _productRepository.CreateProduct("", "", 0, "", "", "");
        var userId = (Guid)Membership.GetUser().ProviderUserKey;
        var item = _itemsRepository.CreateItem(p.ProductId, userId);

        // some code
        return View(model);
    }

Besides, I use Linq to Sql model drag an’ drop approach.
Here is the changed web.config connection string part:

<connectionStrings>
<add name="WebStoreConnectionString" connectionString="Data Source=(LocalDB)\v11.0;
     AttachDbFilename=|DataDirectory|\WebStore.mdf;Integrated Security=True;Connect Timeout=30" 
    providerName="System.Data.SqlClient" />

<add name="DefaultConnection" connectionString="Data Source=|DataDirectory|\aspnet.sdf" 
     providerName="System.Data.SqlServerCe.4.0" />

As I said everything was working before I moved the database to App_Data file. I also tried to remove the dependency between Items and Users tables – the exact same exception.

Any help would be appropriate. Thanks in advance!

Edits:

Ok, now I really broke the dependency between Items and Users tables and no exception occures. But! I have to somehow know who has created each product, so breaking the dependency is not an option. I also tried to remove all code that initializes the CreatedBy field.

Any ideas??

Edits (part 2):

The second comment below gives a great advise! I found that all users that are created are stored now in the aspnet.sdf database!!!
But if I remove the connection string “DeafaultConnection”:

<add name="DefaultConnection" connectionString="Data Source=|DataDirectory|\aspnet.sdf"
  providerName="System.Data.SqlServerCe.4.0" />  

I will get ConfigurationErrorsException:

"The connection name 'DefaultConnection' was not found in the applications 
configuration or the connection string is empty." 

in the folowing line:

 var userId = (Guid)Membership.GetUser().ProviderUserKey;
  • 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-17T08:20:29+00:00Added an answer on June 17, 2026 at 8:20 am

    Ok, as I guessed the issue was in the configuration. Each provider (for some reason) in the connection string had “DefaultConnection”. I changed it to “WebStoreConnectionString”. And now everything works!

    p.s. thanks @w0lf, he pushed the thoughts in the right direction)

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

Sidebar

Related Questions

In my mvc 3 web store app I have this model: Items, Products, Shipping,
I want to develop web store on GAE, that takes data directly from OpenERP
I want my web's register users to store their location (latitude & longitude) from
We've an MVC web app which has a Claim management wizard (similar to a
I am writing a MVC 4 web app and want to use the new
I started porting a simple ASP.NET MVC web app from SQL to RavenDB. I
On my web site (asp.net mvc), I use cookie to store search pattern. when
I need to get image from the web and store it in the phone
I am working on a store locator web app and I have a problem
I want to store events in a web application I am fooling around with

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.