none
Entity Framework Getting started RRS feed

  • Question

  • I'm having trouble getting to first base with the Entity Framework. Below is my sample code, with errors noted as comments at the applicable spot. What am I missing?

    It is run with this code.

    DoStuff d = new DoStuff();
    d.Test();

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;

    namespace EFTest {
        class Misc : DbContext {
            //If constructor not commentd out compile error
            //error = Error 1 The type 'System.Data.Objects.ObjectContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. D:\F\Code\Misc\db\EntityFramework\EFTest\EFTest\Misc.cs 12 16 EFTest
            //yet as seen above System.Data.Entity is referenced and
            //Project references includes reference to Entity Framework 4.1
            //But the metadata for DBContext file of the framework has these lines which show both 4.0 and 4.1
            //#region Assembly EntityFramework.dll, v4.0.30319
            //// C:\Program Files (x86)\Microsoft ADO.NET Entity Framework 4.1\Binaries\EntityFramework.dll
            //#endregion
            //public Misc()
            //    : base("MyCon") {//my connection string in the app config. it works fine elsewhere
            //}

            public DbSet<Father> Fathers { get; set; }
            public DbSet<Son> Sons { get; set; }
        }
        class Father {
            public long Id { get; set; }
            public string Name { get; set; }
            public List<Son> Sons { get; set; }
        }
        class Son {
            public long Id { get; set; }
            public string Name { get; set; }
            public long FatherId { get; set; }
        }
        class DoStuff {
            Misc db = new Misc();
            //this line will not compile at all if uncommented
            //Error 1 Invalid token '(' in class, struct, or interface member declaration D:\F\Code\Misc\db\EntityFramework\EFTest\EFTest\Misc.cs 38 32 EFTest
            //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<Misc>());
            public void Test() {
                try {
                    //if constructor is commented out
                    //Error here  = "CREATE DATABASE permission denied in database 'master'."
                    Father f = db.Fathers.FirstOrDefault();
                    Son s = db.Sons.FirstOrDefault();

                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                    throw;
                }
            }
        }
        //    public class MiscContextInitializer : DropCreateDatabaseIfModelChanges<Misc>{
        //        protected override void  Seed(Misc context)
        //{
        //    Father f = new Father();
        //            f.Name = "Dad";
        //           context.Fathers.Add(f);
        //            base.Seed(context);
        //}
        // }
    }

     

    Monday, September 12, 2011 6:55 PM

Answers

  • Hi Rich;

    The error states to add a reference to System.Data.Entity.dll you have the using statement System.Data.Entity which is also needed.

    In Solution Explorer right click on the Reference node in the project tree and select add a reference. When the new dialog box opens in the .Net tab find and select System.Data.Entity.dl, then recompile and try.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, September 12, 2011 11:23 PM

All replies

  • Hi Rich;

    The error states to add a reference to System.Data.Entity.dll you have the using statement System.Data.Entity which is also needed.

    In Solution Explorer right click on the Reference node in the project tree and select add a reference. When the new dialog box opens in the .Net tab find and select System.Data.Entity.dl, then recompile and try.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, September 12, 2011 11:23 PM
  • Hi Rich,

    It seems you forgot to include the System.Data.Objects. You can add this like: 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    using System.Data.Objects;
    


    Hope This One Can Help.
    Thanks
    To Be Happy Is To Be YourSelf
    Please remember to mark my reply as Proposed as Answers if it's the answer to your question.
    Tuesday, September 13, 2011 4:33 AM
  • As mentioned in the comments in the first post, I have the project reference to System.Data.Entity.dll.

    My intellisense does not pick up the namespace Objects in System.Data.Objects. If I add it anyway it gives a compile error.

    Thursday, September 15, 2011 12:19 PM