Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9168857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:43:56+00:00 2026-06-17T15:43:56+00:00

I am using a partial class (.NET 4.5) on my Entity Framework (v5) object.

  • 0

I am using a partial class (.NET 4.5) on my Entity Framework (v5) object. I added an interface to this partial class, but testing the EF object against this interface is false, but it should be recognized as the interface is defined on the partial class. Here is what I am trying:

public interface Product : ILastModified
{
  public DateTime LastModified { get; set; }
}

Then in my data layer I am trying this:

    public virtual int Update<T>(T TObject) where T : class
    {
        //WHY ALWAYS FALSE?
        if (TObject is ILastModified)
        {
          (TObject as ILastModified).LastModified = DateTime.Now;
        }

        var entry = dbContext.Entry(TObject);
        dbContext.Set<T>().Attach(TObject);
        entry.State = EntityState.Modified;
        return dbContext.SaveChanges();
    }

The problem is that “if (TObject is ILastModified)” is always false even though I set it on the partial class. Am I doing something wrong or is there a way to achieve something like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:43:58+00:00Added an answer on June 17, 2026 at 3:43 pm

    You’ve defined your Product as an Interface instead of a Class.

    Should be:

    interface ILastModified {
    {
        DateTime LastModified { get; set; }
    }
    
    public partial class Product : ILastModified
    {
        /* this prop is declared in the Ef generated class   */
        //public DateTime LastModified { get; set; }
    }
    

    EDIT:

    You don’t have to use Is with this change to your method:

    public virtual int Update<T>(T TObject) where T : class, ILastModified
    {
        TObject.LastModified = DateTime.Now
    
        var entry = dbContext.Entry(TObject);
        dbContext.Set<T>().Attach(TObject);
        entry.State = EntityState.Modified;
        return dbContext.SaveChanges();
    }
    

    and this way you’ll get compile time errors if the type you pass does not implement the interface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Entity Framework 5 code first and ASP.NET MVC 3 . I
I have a ASP.NET MVC 2.0 application using Entity Framework. All my views use
I'm using the ADO.Net Entity Framework. To handle input validation I'm trying to use
I keep getting this error when trying to open a connection using entity framework.
I am building ASP MVC web site using Entity Framework 4.4 with .NET Framework
Using Entity Framework 4.0 in C# / .NET 4.0. Within my Entity model, I
I am developing an ASP.Net MVC 3 Web application using Entity Framework 4.1. I
I am using Entity Framework 5 in VS.NET 2012 using a Database First approach.
I am building an ASP.NET MVC web application using entity framework DbContext using the
I am creating a tempfile using C#. public partial class frmResults : Form {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.