简体   繁体   中英

How can I make it so that the EF updates my ModifiedDate on save?

I have the following using:

using System;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

and the following code:

   public override int SaveChanges() 
    {
    foreach (var stateinfo in this.ChangeTracker.Entries(DataContext)
        .Where(e => e.Entity is StateInfo && (e.State == EntityState.Added || e.State ==  EntityState.Modified))
        .Select(e => e.Entity as StateInfo))
    ) {
        stateinfo.ModifiedDate = DateTime.Now;
       }
    return base.SaveChanges();
    }

However this gives an error and I cannot see why. The error message is that it says

Error 3 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

请导入System.Linq namespace

Since Enumerable.Where<T> method belongs on System.Linq namespace, you need to add it top of your code. From MSDN ;

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)

But.. come on , this is really a basic issue. You can find your solution using Google with just takes a few seconds. Please show a little more effort to solve the problems ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM