3

I was successfully able to save data to the Sql Server database, using an Entity Data Model, as follows:

MEDIANEntities db = new MEDIANEntities();
tblCountry objTable = new tblCountry();
objTable.Name= txtName.Text.Trim();
objTable.CreatedDate = System.DateTime.Now;
db.AddTotblCountries(objTable);
db.SaveChanges();

The idea now, is to use the EDM in a class library, so that it can be consumed in other projects (a tier architecture basically). I have created a Class library - 'MedianContext' and then created a new edmx file inside it - 'MedianModel'. And then another class library - 'MedianDAL'. Added the reference of MedianContext to it.

Unable to access properties of objcontext and tb. How can I proceed further. If it helps, when adding the reference to MedianDAL, the MediaContext.dll was inside the debug folder instead of Release folder, as seen in many examples.

enter image description here

4
  • did you try to use a using-block? Commented Oct 24, 2013 at 13:16
  • Dont know how to use that Commented Oct 24, 2013 at 13:26
  • Heard about LinqToEntities? Commented Oct 24, 2013 at 13:30
  • Using LinqToEntities, I need to first create an object, right?So I did that - objContext...Should I need to add any more references,namespaces Commented Oct 24, 2013 at 13:36

2 Answers 2

1

Have you tried it with Linq2Entities?
e.g.:

try
{
    using (var medianEntities = new MedianModel.MEDIANEntities())
    {
          //Do any LinqToEntity-Expressions
    }
}
catch(Exception)
{ 
    //ErrorHandling
}

That works for me. I also have my .edmx files in a different project and just added a reference to this.

Edit:

Of course you have to put this code into a method-body.
Here is a simple example:

public List<Map> GetAllMaps()
{
    var Maps = new System.Collections.Generic.List<Map>();

    try
    {
         using (var mapEntities = new Model.MapEntities())
         {
              var MyMaps= (from M in mapEntities.Maps
                               orderby M.Description
                               select M.MapID, M.Description);

             foreach (var Map in MyMaps)
             {
                   Maps.Add(Map);
             }
          }
          return Maps;
      }
      catch (System.Exception Exc)
      {
          Log.Err(string.Format("Error: {0}", Exc));
          throw new System.Exception(Exc.ToString());
      }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Putting it into a method worked. I could access the object properties now, even without 'using'. Thank you very much.
Glad to help you. You should take a look at LinqToEntities, in my opinion it's not hard to understand and quite good to handle.
0

Probably because you are trying to access the properties while in the context of the class ? Create constructor for this class and access the properties from there:

public Test
{
tb....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.