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

  • Home
  • SEARCH
  • 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 9215127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:08:03+00:00 2026-06-18T02:08:03+00:00

I have a mvc 3 project and I need to populate sql database with

  • 0

I have a mvc 3 project and I need to populate sql database with data from xml file. So I added the console app project to the solution and wrote the code that will display all needed data on the screen. Now I want to write data into the database. Here is the chunk of code: (fom the console app)

    public static void Main()
    {
        IKernel ninjectKernel = new StandardKernel();
        ninjectKernel.Bind<IItemsRepository>().To<ItemsRepository>();
        ninjectKernel.Bind<IProductRepository>().To<ProductRepository>();
        ninjectKernel.Bind<IShippingRepository>().To<ShippingRepository>();

        var itemsRepository = ninjectKernel.Get<IItemsRepository>(); // redirection to datacontext file
        var productRepository = ninjectKernel.Get<IProductRepository>();
        var shippingRepository = ninjectKernel.Get<IShippingRepository>();

        var doc = XDocument.Load(@"C:\div_kid.xml");
        var offers = doc.Descendants("offer");

        foreach (var offer in offers)
        {   // here I use Linq to XML to get all needed data from xml file: 
            // name, description, price, category, shippingCost, shippingDetails

            Product product = productRepository.CreateProduct(name, description, price, category, "Not Specified", "Not Specified");
            Shipping shipping = shippingRepository.CreateShipping(shippingCost, shippingDetails);

            // here I think I will just create "admin" user and assign its "UserId" to "userId"
            Guid? userId = null;
            Item item = itemsRepository.CreateItem(product.ProductId, shipping.ShippingId, (Guid) userId, DateTime.Now);

            // Resharper highlights this line like unreachable. Why?
            Console.WriteLine("name: {0}", offer.Element("name").Value);
        }
    }

First of all when I run the console app the NullReferenceException occures in MvcProjectName.Designer.cs file in the following line:

public WebStoreDataContext() : 
    base(global::System.Configuration.ConfigurationManager.ConnectionStrings["WebStoreConnectionString"].ConnectionString, mappingSource)                    

NullReferenceException: The reference to the object is not pointing to the object instance.

So, I have lots of questions:

1) How to integrate console app code with mvc 3 app code in one solution?

2) I’ve also found this post on stackoverflow.
But can’t I just add reference to MvcProject in references of ConsoleProject? And this way get access to the “mvc repositories” code?

3) Should I use ninject container in console app?

4) Is there any better implementation of loading data from xml file into slq database? I’ve never had two projects in one solution before, so mabby there are other ways to beautifully handle this situation?

Thanks for Your help in advance!

Edits:

I added app.config file with the connection string:

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

Now when I run console app I get the following SqlException when the Linq to SQL “.submitChanges()” method is called:

An attempt to attach an auto-named database for file C:\Users\Aleksey\repos\working_copy\WebStore\LoadDataTool\bin\Debug\WebStore.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Also in the directory LoadDataTool\bin\Debug “WebStore” file with extension “Program Debug Database” appeared.

  • 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-18T02:08:05+00:00Added an answer on June 18, 2026 at 2:08 am

    It’s hard to make an accurate assumption on your solution architecture, but from what I’m reading, it doesn’t sound like you’ve separated your Data Access Layer (DAL) from the presentation layer (MVC) – which seems to be why you’re trying to referencing it in a console application.

    With that assumption, here’s how I would answer your questions.

    1. I wouldn’t… But if I was going to I was 1) make sure that I have all the required references, 2) validate that the app.config file is setup correctly pointing to the correct database and server.
    2. I would separate your repositories in a different project and use a dependency resolver to inject them into your MVC Application. With that, the console application will only need to reference the DAL assembly – thus not needed all the references in your console app.
    3. If your think that you’re going to be pulling out the DAL in the future, then yes. (**See #2 suggestion).
    4. Unless you can’t run your solution without the XML file and database created, a better solution is to simply make an administration Controller and Action that allows you to upload your XML file and complete the tasks.

    I hope that helps.

    Update

    For your issue

    An attempt to attach an auto-named database for file

    Change your connection string so that it looks something like;

    Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\WebStore.mdf;
    

    A good source for connection strings is:
    http://www.connectionstrings.com/

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

Sidebar

Related Questions

I have an ASP.NET MVC project with xVal and data annotations and I need
I need to separate ViewModels in my MVC project from my business models (Data
I have a MVC 4 project I am currently working on and need some
i have a mixed aspx/MVC webapp project and need to rewrite incoming URL's either
i need to have one project on asp.net mvc 1 but i want to
In my ASP.NET MVC project i need to select all text boxes that have
I have a MVC project that has a Linq to SQL dbml class. It
i have installed ckeditor into my asp.mvc project, now i need to implement just
NET MVC project i have following tag in in web.config file <authentication mode=Forms> <forms
So I have a MVC app and in another project I have a normal

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.