views:

26

answers:

1

Hi,

Recently I started porting ADO.net app to Entity Framework. There are some optional columns in my table. With the ADO.net, I just check for existence of the column and get the value if it is there.

if (MyTable.Columns.Contains("PerformPreCheck") &&
    DBNull.Value != MyRow[MyTable.Columns["PerformPreCheck"]])
{
         m_bPerformPreCheck = (bool)MyRow[MyTable.Columns["PerformPreCheck"]];
}

How can I achieve the same thing with Entity Framework?

Thank you, Suresh

A: 

Presuming your entity is called Foo and the nullable column is called PerformPreCheck:

using(var context = new MyEntities())
{
    var f = context.Foos.First(); // or context.Foos.Where(foo => foo.Id == someId).First(), etc....
    m_bPerformPreCheck = f.PerformPreCheck.GetValueOrDefault();
}
Craig Stuntz
Hi Craig,Thank you for your reply. In my entity model, I have "PerformPreCheck" check coulumn, but in my customer database they do not have that column as it is optional. It is failing at "var f=context.Foos.First()". But the code ADO.net code I gave above works perfectly. I am trying to find the equivalent of it.Thank you,Suresh
jaklucky
You cannot have a DB schema which changes and an Entity Model which doesn't. You must keep your DB schema and Entity Model in sync.
Craig Stuntz
Thank you for the reply. This could quite happen in the field. To me it is like a limitation. I could do this in ADO.net, no problem. I am regretting now moving to Entity Framework.
jaklucky
On second thought, you can't do this in EF 1 (easily), but it wouldn't be hard with a code-only model in EF 4. But every ORM is going to require a mapping of some sort which matches your DB schema.
Craig Stuntz