Problem with 'model first' sample from help file

2 posts, 1 answers
  1. Luc
    Luc avatar
    1 posts
    Member since:
    Nov 2010

    Posted 16 Nov 2010 Link to this post


    I just worked my way through the "Getting Started with Model First Scenario" (appearing in the help file), but I encountered a few issues. I don't know if it matters, but I am running the sample against a SQL Server 2008 Express server, using Visual Studio 2010 Premium.

    A first minor issue is that some of the column data types in the sample are not selectable, but I guess that is a minor issue as equivalent types are available (Int32->int, Varchar->varchar, Bit->bit).

    The database creation script is generated fine, I can execute it, and I can see the three empty tables being created in the database.

    The next minor problem is when entering the (C#) code of the sample: the "MyDatabaseDbContext" class doesn't exist, but a class named "MyDatabaseDbConnection" does exist (the same name used for the connection string in step 5). I assume that that is just a case of not all parts of the sample being consistent.

    I can run the sample, but the results are not what I expected. A problem here is that the sample doesn't clearly state what should have been expected. What I observe is that whenever I run the sample a new row is inserted in the Regions and Students tables, but no row is inserted in the StudentsRegions table. Is that intended? It makes defining that relation table kindof pointless, isn't it?

    Next I have one more question about the sample. How do I insert a second student in the same region without inserting a new region (in an independent execution, so emulating the case where the existing 'studentRegion' variable is not accessible for reuse). And related: what is the normal way to reference a new student to a region that may or may not exist in the databse yet - how do I get that region that may need to be created or may need to be retrieved?

  2. Answer
    Pencho avatar
    22 posts

    Posted 17 Nov 2010 Link to this post

    Hello Luc,

    First, I would like to thank you about your feedback. Straight to your questions:
    You have correctly observed that the types described in the topic are slightly different than the real ones. The Edit Table dialog was changed and we did not have enough time to update the documentation for the Q3 release. But as you guessed, the equivalent types are available. We will update the snapshots and the information for the next release.

    Yes you are right, some parts of the topic are inconsistent. By default if you follow the steps described in the tutorial, the name of the generated context will be the same as the connection string name. In this example, as you said it will be "MyDatabaseDbConnection". However you could easily change the name of the already generated context. For that purpose, in the Visual Designer, right-click on an empty area and select "Show Model Settings...". In the Model Settings tab page (the first tab page in the dialog), there is only one setting named "Model Name", this is the name of the context. Once you change it, click Ok and Save the model. That should be enough to change the name of the context.

    About the next question, I completed the tutorial step-by-step and executed exactly the same code as the one listed in the topic. Everything should work correctly. Three records should be added in the database - one in the Region table, one in the Student table and one in the StudentsRegions join table.

    About the last scenario. Suppose you have already inserted region record(s) in the database. In order to add a new student to an existing region, first you have to load the target region from the database. For that purpose you have to write a linq query against the context. Next, you have to assign the region to the new student, by using the student.Regions collection. Finally, pass the student to the context by invoking the dbContext.Add method and save the changes. Here is a sample code demonstrating how to do that:

    MyDatabaseDbContext dbContext = new MyDatabaseDbContext();
    Region region = (from r in dbContext.Regions
                     where r.Name == "USA"
                     select r).FirstOrDefault();
    Student student = new Student();
    student.IsActive = true;
    student.Name = "NewStudent";

    Please see this topic, it should give you basic information about how to work with data in OpenAccess ORM:
    For more information about writing linq queries and loading data from the database, please refer to the following sections in the help documentation:

    Please if you have further questions or need more assistance, do not hesitate to contact us.

    Sincerely yours,

    the Telerik team


    See What's New in Telerik OpenAccess ORM in Q3 2010!
    Monday, November 15, 11 am Eastern Time: Register here>>
    Monday, November 15, 10 pm Eastern Time: Register here>>
  3. DevCraft banner
Back to Top