locked
Can't using Linq to entity in new library RRS feed

  • Question

  • Hi friends,

    I have a trouble with using linq to entity.

    I have using vs2012, entity framework5.

    Please read the following scenarios:

    First, I creat a new class library(name as DemoDAL) and then add a edmx file, which genereated by database, now in this library I can use the entities as below:

    using(DemoEntities ctx = new DemoEntities()){
        var query = from q in ctx.testtb
                    select q;
    }

    Second, I have created a new class library(name as DemoBLL) and then add a class file, I have reference the DemoDAL namespace, but when I use the above code, I have got the following error:

     type used in a using statement must be implicitly convertible to 'System.IDisposable'

    I have search in google long time, but not solution.

    So, please help me.

    Thanks.



    Saturday, November 9, 2013 3:35 PM

Answers

  • Hello,

    In Entity Framework 5 model first, there seem to be some breaking changes due to the way the class files are generated (No more code generation, but T4 templates)

    2 examples:

    The generated context file doesn't implement IDisposable anymore

    There isn't a constructor which takes a connectionstring anymore

    So we need to implement IDisposable in the subclass as well. Try the code as below:

    namespace DemoDAL
    
    {
    
        public partial class DataBaseFirstDBEntities : IDisposable
    
        {
    
            public void Dispose()
    
            {
    
                base.Dispose();
    
            }
    
        }
    
    }

    In DemoDAL, to add a partial class of the sub dbcontext.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Fred Bao Monday, November 11, 2013 7:20 AM
    • Marked as answer by Fred Bao Monday, November 18, 2013 8:16 AM
    Monday, November 11, 2013 7:20 AM

All replies

  • Hello,

    In Entity Framework 5 model first, there seem to be some breaking changes due to the way the class files are generated (No more code generation, but T4 templates)

    2 examples:

    The generated context file doesn't implement IDisposable anymore

    There isn't a constructor which takes a connectionstring anymore

    So we need to implement IDisposable in the subclass as well. Try the code as below:

    namespace DemoDAL
    
    {
    
        public partial class DataBaseFirstDBEntities : IDisposable
    
        {
    
            public void Dispose()
    
            {
    
                base.Dispose();
    
            }
    
        }
    
    }

    In DemoDAL, to add a partial class of the sub dbcontext.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Fred Bao Monday, November 11, 2013 7:20 AM
    • Marked as answer by Fred Bao Monday, November 18, 2013 8:16 AM
    Monday, November 11, 2013 7:20 AM
  • Hi Fred Bao,

    I have follow your reply, but got the following error:

    Could not load file or assembly 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    Tuesday, November 12, 2013 9:16 AM
  • Do you have installed the Entity Framework in DemoDAL library?

    If havent, please install it.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 12, 2013 9:19 AM