none
Looking for example regarding calculated or computed Columns in Entity Framework Code First RRS feed

  • Question

  • suppose my model has full name property but table has no full name field. so tell me how to have full name computed property which will return first name and last name jointly as full name. i got a sample code from this link http://geekswithblogs.net/DavidPaquette/archive/2012/09/23/calculated-columns-in-entity-framework-code-first-migrations.aspx but do not understand how to use it in code

    just guide me how to use it

       1: public string FullName 
    
       2: {
    
       3:     get { return string.Format("{0} {1}", FirstName, LastName); }
    
       4: }

    thanks

    Friday, September 16, 2016 2:28 PM

Answers

  • Hi Mou_inn,

    According to your description and related code, I suggest that you could Data Transfer Objects (DTOs) to achieve it. like this:

    #Dto

    namespace DtosDemo
    {
        public class EmpDto
        {
            public int Id { get; set; }
    
            public string FullName { get; set; }
    
        }
    }
    

    #Usage:

     using (var db = new EFDemo())
                {
                    var query = db.Employee.Select(t => new EmpDto
                    {
                        Id = t.Id,
                        FullName = t.FirstName +" "+ t.LastName
                    });
    
                    foreach (var item in query)
                    {
                        Console.WriteLine("Id: {0} -- FullName: {1}", item.Id, item.FullName);
                    }
    
                    Console.ReadKey();
                }

    For more information, please refer to:

    http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Sudip_inn Wednesday, September 21, 2016 8:04 AM
    Monday, September 19, 2016 3:14 AM
    Moderator
  • i got my answer after few search. here it is.

    public class FirstTable
    {
        [Key]
        public int UserID { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]      
        public string Summ
        {
            get { return /* do your sum here */ }
            private set { /* needed for EF */ }
        }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]          
        public string FullName
        {
            get { return string.Format("{0} {1}", FirstName, LastName); }
        }
    }


    =================================

    public class UserProfile
    {
        public int Id { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public string FullName
        {
        get { return FirstName + "; " + LastName; }
        private set
        {
        //Just need this here to trick EF
        }
        }

    }

    public class UserContext : DbContext
    {
        public DbSet Users { get; set; }
    }


    using(UserContext context = new UserContext())
    {
        UserProfile userProfile = new UserProfile {FirstName = "Chanandler", LastName = "Bong"};

        Console.WriteLine("Before saving: " + userProfile.FullName);

        context.Users.Add(userProfile);
        context.SaveChanges();

        Console.WriteLine("After saving: " + userProfile.FullName);

        UserProfile chanandler = context.Users.First(u => u.FullName == "Chanandler Bong");
        Console.WriteLine("After reading: " + chanandler.FullName);

        chanandler.FirstName = "Chandler";
        chanandler.LastName = "Bing";

        Console.WriteLine("After changing: " + chanandler.FullName);

    }

    code taken from here http://www.davepaquette.com/archive/2012/09/23/calculated-columns-in-entity-framework-code-first-migrations.aspx

    few more relevant links http://daveaglick.com/posts/computed-properties-and-entity-framework
    https://msdn.microsoft.com/en-us/data/jj591583.aspx
    http://csharpcode.org/specify-computed-column-in-entity-framework-code-first/
    http://lancelarsen.com/entity-framework-code-first-computed-getdate/

    • Marked as answer by Sudip_inn Wednesday, September 21, 2016 8:04 AM
    Wednesday, September 21, 2016 8:04 AM

All replies

  • So why can't you just use a separate object for this that has nothing to do with a model object.

    Or use partial classes...

    https://msdn.microsoft.com/en-us/library/wa80x488.aspx?f=255&MSPPError=-2147217396

    https://www.dotnetperls.com/partial

     
    Friday, September 16, 2016 3:30 PM
  • Hi Mou_inn,

    According to your description and related code, I suggest that you could Data Transfer Objects (DTOs) to achieve it. like this:

    #Dto

    namespace DtosDemo
    {
        public class EmpDto
        {
            public int Id { get; set; }
    
            public string FullName { get; set; }
    
        }
    }
    

    #Usage:

     using (var db = new EFDemo())
                {
                    var query = db.Employee.Select(t => new EmpDto
                    {
                        Id = t.Id,
                        FullName = t.FirstName +" "+ t.LastName
                    });
    
                    foreach (var item in query)
                    {
                        Console.WriteLine("Id: {0} -- FullName: {1}", item.Id, item.FullName);
                    }
    
                    Console.ReadKey();
                }

    For more information, please refer to:

    http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Sudip_inn Wednesday, September 21, 2016 8:04 AM
    Monday, September 19, 2016 3:14 AM
    Moderator
  • i got my answer after few search. here it is.

    public class FirstTable
    {
        [Key]
        public int UserID { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]      
        public string Summ
        {
            get { return /* do your sum here */ }
            private set { /* needed for EF */ }
        }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]          
        public string FullName
        {
            get { return string.Format("{0} {1}", FirstName, LastName); }
        }
    }


    =================================

    public class UserProfile
    {
        public int Id { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public string FullName
        {
        get { return FirstName + "; " + LastName; }
        private set
        {
        //Just need this here to trick EF
        }
        }

    }

    public class UserContext : DbContext
    {
        public DbSet Users { get; set; }
    }


    using(UserContext context = new UserContext())
    {
        UserProfile userProfile = new UserProfile {FirstName = "Chanandler", LastName = "Bong"};

        Console.WriteLine("Before saving: " + userProfile.FullName);

        context.Users.Add(userProfile);
        context.SaveChanges();

        Console.WriteLine("After saving: " + userProfile.FullName);

        UserProfile chanandler = context.Users.First(u => u.FullName == "Chanandler Bong");
        Console.WriteLine("After reading: " + chanandler.FullName);

        chanandler.FirstName = "Chandler";
        chanandler.LastName = "Bing";

        Console.WriteLine("After changing: " + chanandler.FullName);

    }

    code taken from here http://www.davepaquette.com/archive/2012/09/23/calculated-columns-in-entity-framework-code-first-migrations.aspx

    few more relevant links http://daveaglick.com/posts/computed-properties-and-entity-framework
    https://msdn.microsoft.com/en-us/data/jj591583.aspx
    http://csharpcode.org/specify-computed-column-in-entity-framework-code-first/
    http://lancelarsen.com/entity-framework-code-first-computed-getdate/

    • Marked as answer by Sudip_inn Wednesday, September 21, 2016 8:04 AM
    Wednesday, September 21, 2016 8:04 AM