---
title: "MVC4 Entity Framework - Set Field value before Insertion/Update"  
description: "MVC4 Entity Framework - Set Field value before Insertion/Update"  
author: "Anonymous User"  
published: 2013-09-28  
updated: 2013-09-28  
canonical: https://www.mindstick.com/forum/1547/mvc4-entity-framework-set-field-value-before-insertion-update  
category: "ado.net"  
tags: ["ado.net"]  
reading_time: 2 minutes  

---

# MVC4 Entity Framework - Set Field value before Insertion/Update

I am working with an MVC 4 [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) [website](https://www.mindstick.com/articles/13110/why-copywriting-is-crucial-for-new-website-build) using [Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach). This is my first ASP.NET website and sorry if I don't make much sense.

The website hosts posts, just think of an [WordPress](https://www.mindstick.com/articles/12248/franco-elegant-woocommerce-wordpress-theme) like blog. I expect to have a title [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) and a slug property which is implicitly [derived](https://www.mindstick.com/interview/110/what-s-the-top-dot-net-class-that-everything-is-derived-from) from the title. So this is the class for the 'Post'.

\

```
public class Post
{
    public virtual int PostID { get; set; }
   public virtual String Title { get; set; }
   public virtual string Slug { get; set; }
}
```

\

How could I set 'Slug' when inserting/updating the entry. I assume I have to tap in to the [method](https://www.mindstick.com/forum/166/webservice-method) that [updates](https://yourviews.mindstick.com/view/85235/global-efforts-to-combat-climate-change-updates-and-initiatives)/inserts the database. It seems to be hiding. I am wondering if [annotations](https://www.mindstick.com/articles/12141/annotations-in-java-target-and-retention) could do the trick.

Apart from that and auto generated forms, I have a [DbContext](https://www.mindstick.com/forum/160257/what-is-the-role-of-the-dbcontext-class-in-database-migrations-with-entity-framework-core)extended class with theOnModelCreating implemented.

## Replies

### Reply by Anonymous User

Hey Ashish!

Based on that code, the Slug property isn't being derived from anything. If you want it to be derived from the title, you could try something like this:

```
private string title;
public virtual string Title {
    get { return title; }
    set {
        title = value;
        Slug = title.replace(" ", "-");
    }
}
public virtual string Slug { get; private set; }
```


---

Original Source: https://www.mindstick.com/forum/1547/mvc4-entity-framework-set-field-value-before-insertion-update

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
