locked
Help Getting Started with LINQ/Entity Framework RRS feed

  • Question

  • I decided to give the Entity Framework and LINQ a try. I'm not new to .NET, but I am very new to these technologies. Naturally, I'm getting nowhere in a hurry.

    Here's what I did:

    1. I right clicked on my WinForms project, selected Add New Item and chose "ADO.NET Entity Data Model".

    2. I was then able to generate an EDMX file from my SQL Server 2008 database.

    3. I then tried to write my first LINQ statement against the created entities.

    Here's the problems I'm having:

    1. The namespace for my model is blackbeltcoderModel, yet I get no Intellisense for this name. And, if I type it in, I get an error saying the type or namespace could not be found.

    2. I tried entering the statement below. bbc_Categories, the name of one of my tables, appears in pale blue indicating it is recognized. However, I get the error "Could not find an implementation of the query pattern for source type 'BlackBelt.bbc_Categories'.  'Where' not found."

    var x = from c in bbc_Categories
    	where c.Title.Length > 0
    	select c.Title;

    Do I need to do more to create the required objects? Can someone help me fill in the missing pieces?

    Thanks.


    Jonathan Wood | SoftCircuits | Developer Blog
    Saturday, June 26, 2010 9:15 PM

Answers

  • Hi Jonathan,

     

    Thank you very much for sending me the demo!   I found where the problem is. 

     

    bbc_Categories is a EntityObject type class.   To use LINQ to Entities, we need to first create an ObjectContext class, which manages all the entities classes, connections, transactions, and etc.    The ObjectContext can be seen as a database.  Then ObjectContext has several ObjectSet<> collection, these ObjectSet<> can be seen as data tables.   I modified your codes in the demo into:

    =============================================================================

                using (blackbeltcoderEntities context = new blackbeltcoderEntities())

                {

                    var x = from c in context.bbc_Categories

                            where c.CatTitle.Length > 0

                            select c.CatTitle;

                }

    =============================================================================

    If you have any questions, please feel free to let me know.

     

    Good day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, July 5, 2010 8:19 AM

All replies

  • Hi,
    About the namespace issue, EF's generated files are created with a namespace which you can examine in the [edmx file name].desinger.cs file. Check there what is the generated namespace.
    Also, it seems that you are not referencing the System.Linq assembly from the error you get about the 'Where'.
    Check if the following statement is missing in your cs files:

    using

     

    System.Linq;

    I hope it will help you.


    Gil Fink
    Sunday, June 27, 2010 5:17 AM
  • Right, the namespace used is the same as my application is using, which appears to be why it recognized all the EF objects just fine. I guess I'm just confused about the namespace property--it appears to have no effect.

    I already have a using statement for System.Linq.


    Jonathan Wood | SoftCircuits | Developer Blog
    Sunday, June 27, 2010 3:34 PM
  • Hello Jonathan,

     

    How is the problem now?  Do you still have the problem when using EF?  If so, please feel free to let us know.

     

    BTW, welcome to the wonderful world of EF and LINQ!   J

     

    Good day, both!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 29, 2010 8:37 AM
  • Yes, I still have the same problem I originally described. I've been searching the web but have not been able to find a solution.

    Many of the articles I've found on the web were related to missing the using statement, as Gil suggested. But that does not appear to be the problem in my case.

    As I mentioned, I have a LINQ/FE book but it's pre-VS2010. I've ordered a newer book and was going to come back to this problem once I start reading that.


    Jonathan Wood | SoftCircuits | Developer Blog
    Tuesday, June 29, 2010 2:41 PM
  • Hi Jonathan,

     

    If it is convenient for you, could you please send me a demo project for this issue?   My mail: v-micsun@microsoft.com.   I will do my best to help!

     

    Good day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, June 30, 2010 9:07 AM
  • Hi Jonathan,

     

    Thank you very much for sending me the demo!   I found where the problem is. 

     

    bbc_Categories is a EntityObject type class.   To use LINQ to Entities, we need to first create an ObjectContext class, which manages all the entities classes, connections, transactions, and etc.    The ObjectContext can be seen as a database.  Then ObjectContext has several ObjectSet<> collection, these ObjectSet<> can be seen as data tables.   I modified your codes in the demo into:

    =============================================================================

                using (blackbeltcoderEntities context = new blackbeltcoderEntities())

                {

                    var x = from c in context.bbc_Categories

                            where c.CatTitle.Length > 0

                            select c.CatTitle;

                }

    =============================================================================

    If you have any questions, please feel free to let me know.

     

    Good day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, July 5, 2010 8:19 AM
  • Thanks much for taking a look. I've looked at numerous examples that didn't include the missing part and posted my own code that didn't have it too. I thought I was missing something like that. (For me, it's actually BlackBelt.Entities but yes that appears to do the trick.)

    Anyway, I can now move forward with trying to learn this stuff. Thanks again!

     


    Jonathan Wood | SoftCircuits | Developer Blog
    Monday, July 5, 2010 2:34 PM
  • Okay, so my code now compiles and I spent a few more minutes with it. I'm now trying to populate a combo box with the results of my query and I'm getting nowhere fast.

    private void EditArticle_Load(object sender, EventArgs e)
    {
    	using (BlackBelt.Entities context = new BlackBelt.Entities())
    	{
    		var x = from c in context.bbc_Categories
    				where c.CatTitle.Length > 0
    				select new { c.CatID, c.CatTitle };
    
    		foreach (var y in x)
    			cboCategory.Items.Add(y);
    
    //		cboCategory.DataSource = x; // This has no effect as well
    	}
    }

    This code places zero items in my cboCategory combo box. The commented line does the same thing. So I tried stepping through in the debugger. I hit the using statement. But, after that, I jump to a line related to Entities and if I step again, it skips right over my code that is loading the form. There seems to be no way to actually step through any of the code inside my using block.

    This is certainly frustrating because I'm already up to speed using SQL. I know that even if this stuff worked for me, it wouldn't be as fast. So I'm really close to dropping it. Perhaps there's still something I'm missing. Nothing wants to work for me.

    And, finally, I definitely appreciate Lingzhi's help, but I'm still at a lost. If I look at the examples online at places like http://mosesofegypt.net/post/LINQ-to-Entities-what-is-not-supported.aspxhttp://vbcity.com/blogs/xtab/archive/2010/02/01/a-simple-example-of-using-linq-and-entity-framework.aspx and most others, they don't need to create a context.

    I guess none of this is adding up for me. I may just have to drop LINQ/EF and get back to developing my site.

    Thanks.


    Jonathan Wood | SoftCircuits | Developer Blog
    Monday, July 5, 2010 4:20 PM
  • I figured out my main problem there. The connection string was wrong and I'm not sure why I didn't get any kind of an error.

    It's confusing with a bunch of stuff going on behind the scenes and I don't know enough to set, change, or even view the connection string being used.

    Anyway, by letting VS save it's own connection string with its own name, the code is now working and I'm actually able to read data.

    I'll continue playing with it.

    Thanks.


    Jonathan Wood | SoftCircuits | Developer Blog
    Monday, July 5, 2010 7:46 PM