Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

How to update entity without fetching it first

$
0
0

Hi,

I've requirement to update entity without selecting it first to avoid 2 database hits. I've googled and read a lot on this topic, all say to use Attach method but that does not seem working if my table has non nullable columns and I try to update entity without passing values in them.

My [User] table in database has Id (primary key), Name (Not NULL), Image (NULL) columns. Now if I try to edit the row using below sample code then it gives entity framework validation error for Name field that"The field Name is required". I am using Entity Framework 6.1

using(DBEntities DB = new DBEntities())
{
  User user = new User(){ Id = 1 };
  DB.Users.Attach(user);
  user.Image = "";
  DB.SaveChanges();
}

Thanks,
Vikas


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>