MVC3 EF4 POCO Repository/UnitOfWork Connection Error

I implemented the T4 Repository/ Unit of Work templates by Gil Fink for use in a project I am working on, my first full scale project using MVC. I am, however, getting an error I wasn't getting before, and I can't track it down. I don't know if it's something with the templates, or just a setting somewhere I have set wrong, but I am at a lose right now. I was hoping someone would be able to shed some light on the situation.

Here's my framework setup:

  • MVC 3 Beta
  • SQL Server 2008 R2
  • Ninject v2.1.0.76
  • EF4 POCO
  • 3 projects in the solution: Data, Entities and the MVC app.
  • I am doing a DB first design, and using EF to create the POCO classes, via Microsoft's ADO.NET POCO Entity Generator. I then use the T4 tool to create the repository and unit of work patterns. With that setup, and all the classes and repositories generated, I implement it into the MVC app using Ninject for DI. I am using the MVC 2 method using a Controller Factory at this point, with plans to later change it to the IDependencyResolver method.

    When I use a hard-coded Mock repository, the application works as it should, however when I change it to use the IRepository binding, I get the following error: "The supplied connection is not valid because it contains insufficient mapping or metadata information. Parameter name: connection" This indicates to me that the connection string for EF to connection to the DB is incorrect, however it is the default string generated by the ADO.NET Entity Data Model template. Perhaps it is also something with the .edmx settings.

    Here is my connection string (using the handy Nerd Dinner database layout)

    <add name="NerdDinnerEntities"
    connectionString="metadata=
    res://*/Model1.csdl|
    res://*/Model1.ssdl|
    res://*/Model1.msl;
    provider=System.Data.SqlClient;
    provider connection string=&quot;Data Source=Wayne;Initial Catalog=NerdDinner;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True&quot;"
    providerName="System.Data.EntityClient" />
    

    Anyone with any thoughts/ hints, etc, I would be extremely appreciative.

    Edit: here's the link for the T4 template I'm using: Repository and Unit of Work T4 Template for Entity Framework

    Edit2: The error is something to do with home I'm using DI with Unit Of Work. when I remove DI, and manually have the dependencies in the controllers, it works. When I try to implement DI, it breaks.


    res://*/Model1.csdl|
    

    That * is a wildcard that says to EF "scan all the assemblies for the resource". Chances are this scan isn't finding the assembly for whatever reason.

    Change * to your assembly name:

    res://My.Assembly.Name/Model1.csdl|
    

    If you are using NuGet to install your Ninject dependency, it likes to set up your DI bindings in NinjectWebCommon.cs . If loading your DI bindings requires an Entity Framework context to be instantiated, this happens too early in the application lifecycle and the application can't interpret the connection string properly.

    If you think this is may be what's happening to you, see my answer here for more information.

    链接地址: http://www.djcxy.com/p/48594.html

    上一篇: 拓扑排序与分组

    下一篇: MVC3 EF4 POCO存储库/ UnitOfWork连接错误