如何在一个解决方案中将控制台应用程序与现有的asp.net mvc 3应用程序集成?

时间:2013-01-28 14:09:08

标签: asp.net asp.net-mvc asp.net-mvc-3 architecture

我有一个mvc 3项目,我需要使用xml文件中的数据填充sql数据库。所以我将控制台应用程序项目添加到解决方案中并编写了代码,该代码将在屏幕上显示所有需要的数据。现在我想将数据写入数据库。这是代码块:(从控制台应用程序开始)

    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);
        }
    }

首先,当我运行控制台应用程序时,NullReferenceException出现在以下行的MvcProjectName.Designer.cs文件中:

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

NullReferenceException:对象的引用未指向对象实例。

所以,我有很多问题:

1)如何在一个解决方案中集成控制台应用程序代码和mvc 3应用程序代码?

2)我还在stackoverflow上找到了this post。 但是我不能在ConsoleProject的引用中添加对MvcProject的引用吗?这种方式可以访问“mvc存储​​库”代码吗?

3)我应该在控制台应用程序中使用ninject容器吗?

4)是否有更好的实现将数据从xml文件加载到slq数据库?我以前从来没有在一个解决方案中有两个项目,所以mabby有其他方法可以很好地处理这种情况吗?

提前感谢您的帮助!

编辑:

我添加了带有连接字符串的app.config文件:

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

现在,当我运行控制台应用程序时,当调用Linq to SQL“.submitChanges()”方法时,我得到以下SqlException:

尝试为文件C:\ Users \ Aleksey \ repos \ working_copy \ WebStore \ LoadDataTool \ bin \ Debug \ WebStore.mdf附加自动命名的数据库失败。存在具有相同名称的数据库,或者无法打开指定的文件,或者它位于UNC共享上。

同样在目录LoadDataTool \ bin \ Debug中出现“WebStore”文件,扩展名为“Program Debug Database”。

2 个答案:

答案 0 :(得分:1)

我想您需要在控制台应用程序使用的App.config文件中定义连接字符串(与您在web.config中使用的方式相同):

<connectionStrings>
    <add 
        name="WebStoreConnectionString" 
        connectionString="YOUR CONNECTION STRING COMES HERE" 
        providerName="System.Data.SqlClient"
    />
</connectionStrings>

答案 1 :(得分:1)

很难对您的解决方案架构做出准确的假设,但从我正在阅读的内容来看,听起来并不像您已将数据访问层(DAL)与演示文稿分开层(MVC) - 这似乎是您尝试在控制台应用程序中引用它的原因。

有了这个假设,我就会回答你的问题。

  1. 我不会......但是如果我要去的话1)确保我拥有所有必需的引用,2)验证app.config文件是否正确设置指向正确的数据库和服务器。
  2. 我会在不同的项目中分离您的存储库,并使用依赖项解析程序将它们注入到您的MVC应用程序中。这样,控制台应用程序只需要引用DAL程序集 - 因此不需要控制台应用程序中的所有引用。
  3. 如果您认为将来要退出DAL,那么是的。 (**见#2建议)。
  4. 除非您在没有创建XML文件和数据库的情况下无法运行解决方案,否则更好的解决方案是简单地创建一个管理控制器和操作,允许您上传XML文件并完成任务。
  5. 我希望有所帮助。

    <强>更新

    针对您的问题

      

    尝试为文件附加自动命名的数据库

    更改连接字符串,使其看起来像;

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

    连接字符串的一个很好的来源是: http://www.connectionstrings.com/

相关问题