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

  • SEARCH
  • Home
  • 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 8212307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:42:40+00:00 2026-06-07T10:42:40+00:00

I use EF 4.3.1 I need adding a Default Value of an entity at

  • 0

I use EF 4.3.1 I need adding a Default Value of an entity at the moment of saving.

At the moment I’m using SaveChanges() and it works.

As you can see from the code I use ChangeTracker.Entries<Option>() where Option rappresent a specifc DataType.

I would like to know if is possible and how to write a more generic version of ChangeTracker.Entries something like ChangeTracker.Entries<t>() so it can check all type of entities in my Model, as you can see in my specif case I have a NoteInternal property on many classes

      public override int SaveChanges()
    {
        #region Option BL
        var entities = ChangeTracker.Entries<Option>()
                                    .Where(e => e.State == EntityState.Added ||
                                                e.State == EntityState.Modified)
                                    .Select(e => e.Entity);
        // Add Default values when Creating or Editing an Entity
        string defaultvalue = "";
        foreach (var entity in entities)
        {
            if (String.IsNullOrWhiteSpace(entity.NoteInternal))
                entity.NoteInternal = defaultvalue;
        }
        #endregion

        return base.SaveChanges();
    }

Thanks for your help!

PS: does it make sense use the DYNAMIC datatype here?

  • 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-07T10:42:42+00:00Added an answer on June 7, 2026 at 10:42 am

    I can think of three options:

    1. Use reflection to check if each item has a NoteInternal property. This is slow.
    2. Use dynamic to try to access the NotIntenral property of each item. You have to check for and handle the exception thrown when the entity does not have the property.
    3. Make an interface IHasNoteInternal that defines the property and have all entities that has the property implement that interface. Then get all entries of IHasNoteInternal type.

    I think that the 3. option is best and gives the best design, but it requires you to change the entities which migh or might not be possible. I don’t know if you can use the generic Entries<TEntity>() method to get the changed entities – it might fail to match the entities based on an interface. In that case, use the non-generic Entries() and use linq’s OfType<T>() operator to filter out the relevant entries.

    var hasNotInternal = ChangeTracker.Entries()
                         .Select(e => e.Entity).OfType<IHasNotInternal>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use the web services available by adding /_vti_bin/webs.asmx to your site
I need use this code: <%= button_tag :class => btn btn-primary do %> <%=
So, I need use this event so I can navigate trought blog posts. I
For some reasons I can't use xml layout files. But I need to create
I need to make changes to an in-use production database. Just adding a few
What i need to do is customize my default userForm by adding a 'passwordagain'
I usually use this code below to include the page that I need into
I need use this method with three distinct classes: Orders, Customers, Suppliers public void
I have installed Matlab r2010a on my computer I need use the function xlsread
I need to use maps on running in a physical Android device, how do

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.