How to integrate console app with the existing asp.net mvc 3 app in one solution?

huyyetra920

New Member
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)\[code\] 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); } }\[/code\]First of all when I run the console app the NullReferenceException occures in MvcProjectName.Designer.cs file in the following line: \[code\]public WebStoreDataContext() : base(global::System.Configuration.ConfigurationManager.ConnectionStrings["WebStoreConnectionString"].ConnectionString, mappingSource) \[/code\]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: \[code\]<add name="WebStoreConnectionString" connectionString="Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\WebStore.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>\[/code\]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.
 
Back
Top