Home > DeveloperSection > Forums > MVC4 Entity Framework - Set Field value before Insertion/Update
Ashish Pandey

Total Post:128

Posted on    September-28-2013 4:08 AM

 ADO.Net ADO.Net 

 1 Reply(s)
 1505  View(s)
Rate this:
I am working with an MVC 4 ASP.NET website using Entity Framework. 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 like blog. I expect to have a title property and a slug property which is implicitly derived 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 that updates/inserts the database. It seems to be hiding. I am wondering if annotations could do the trick.

Apart from that and auto generated forms, I have a DbContextextended class with theOnModelCreating implemented.

Pravesh Singh
Pravesh Singh

Total Post:101

Posted on    September-28-2013 6:38 AM

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; }

Don't want to miss updates? Please click the below button!

Follow MindStick