Question

I have an entity called Service like following:

public class Service
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int serviceId { get; set; }
    public string name { get; set; }
    public string description { get; set; }

    public ServiceCategory category { get; set; }
}

The category is chosen by the user through a drop down that holds the id and the name of the categories available in the db. the ViewModel will then return that id to me to save. The way I've been doing this so far is by going to my repository, getting the category object where the id matches the selected category in the view and then assigning it. This of course takes a round trip to the db.

I was wondering, since I already have the id, is there a better way of doing this where I don't go to the db?

Was it helpful?

Solution

modify your code to

public class Service
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int serviceId { get; set; }
public int ServiceCategoryId {get; set;}  // Added
public string name { get; set; }
public string description { get; set; }

public ServiceCategory ServiceCategory { get; set; }
}

and fill ServiceCategoryId.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top