Add variable to Entity Framework created model class without editing the actual database columns?












0















I just recently started creating a .net web app in mvc5. I've tried to search for the answer as I usually do rather than asking a question but Im not sure how to word this but here it goes:



I have a class:



public partial class employee
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}


In which the class was created by using the Database First wizard with Entity Framework. (reasoning for the tag)



What I did was get the FistName, MiddleName, and LastName from the database. In which I successfully did with this ActionResult from my controller.



[ChildActionOnly]
public ActionResult Nav()
{
employee user;
using (DatabaseEntities context = new DatabaseEntities())
{
user = context.employees.FirstOrDefault(e => e.FirstName ="Bob");
if (!System.IO.File.Exists(Server.MapPath(user.imagefile)))
{
user.imagefile = $"/Content/Images/user/{user.FirstName[0]}.png";
}
}
return PartialView("_Nav", user);
}


Now I'm wondering if its possible, how to create another item to the class without adding a column to the database. I want to add a FormattedName variable to the class that is got after the database call to get the model (user) to include the new variable (FormattedName) that gets its value from the existing variables in the class.



I understand the wording and terminology could be off but any help would be great and thx in advance.



Edit:



In the comment section, I was informed about attributes ex.[NotMapped] which was information I definitely needed to know. But for this particular question, it doesn't work. Everything was ok until I updated the model using entity framework, the attributes were gone and have to keep adding them every model update which is not ideal for me. I went ahead and edited my database to include a (FormattedName) column so the problem is fixed. But I still would love to know if this is doable.










share|improve this question




















  • 3





    You can use the NotMapped attribute, but its better to create a view model representing what you want in the view.

    – user3559349
    Nov 20 '18 at 0:19













  • In which class you wanted to add your new item? You can add it before return PartialView("_Nav", user);

    – Vijunav Vastivch
    Nov 20 '18 at 0:26













  • @StephenMuecke The [NotMapped] attribute worked perfectly. Thank you. But can you explain how and why is it best to create a view model representing the view for better understanding? Or would I have to post another question?

    – Bigg_aye
    Nov 20 '18 at 0:40








  • 1





    Start with What is ViewModel in MVC?. And if you editing data, it should be mandatory to use a view model (some of the reasons are explained in that link, but there are others such as [What does it mean for a property to be [Required] and nullable?](stackoverflow.com/questions/43688968/…)

    – user3559349
    Nov 20 '18 at 0:45











  • @StephenMuecke thx for the info. Appreciate your time.

    – Bigg_aye
    Nov 20 '18 at 0:51
















0















I just recently started creating a .net web app in mvc5. I've tried to search for the answer as I usually do rather than asking a question but Im not sure how to word this but here it goes:



I have a class:



public partial class employee
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}


In which the class was created by using the Database First wizard with Entity Framework. (reasoning for the tag)



What I did was get the FistName, MiddleName, and LastName from the database. In which I successfully did with this ActionResult from my controller.



[ChildActionOnly]
public ActionResult Nav()
{
employee user;
using (DatabaseEntities context = new DatabaseEntities())
{
user = context.employees.FirstOrDefault(e => e.FirstName ="Bob");
if (!System.IO.File.Exists(Server.MapPath(user.imagefile)))
{
user.imagefile = $"/Content/Images/user/{user.FirstName[0]}.png";
}
}
return PartialView("_Nav", user);
}


Now I'm wondering if its possible, how to create another item to the class without adding a column to the database. I want to add a FormattedName variable to the class that is got after the database call to get the model (user) to include the new variable (FormattedName) that gets its value from the existing variables in the class.



I understand the wording and terminology could be off but any help would be great and thx in advance.



Edit:



In the comment section, I was informed about attributes ex.[NotMapped] which was information I definitely needed to know. But for this particular question, it doesn't work. Everything was ok until I updated the model using entity framework, the attributes were gone and have to keep adding them every model update which is not ideal for me. I went ahead and edited my database to include a (FormattedName) column so the problem is fixed. But I still would love to know if this is doable.










share|improve this question




















  • 3





    You can use the NotMapped attribute, but its better to create a view model representing what you want in the view.

    – user3559349
    Nov 20 '18 at 0:19













  • In which class you wanted to add your new item? You can add it before return PartialView("_Nav", user);

    – Vijunav Vastivch
    Nov 20 '18 at 0:26













  • @StephenMuecke The [NotMapped] attribute worked perfectly. Thank you. But can you explain how and why is it best to create a view model representing the view for better understanding? Or would I have to post another question?

    – Bigg_aye
    Nov 20 '18 at 0:40








  • 1





    Start with What is ViewModel in MVC?. And if you editing data, it should be mandatory to use a view model (some of the reasons are explained in that link, but there are others such as [What does it mean for a property to be [Required] and nullable?](stackoverflow.com/questions/43688968/…)

    – user3559349
    Nov 20 '18 at 0:45











  • @StephenMuecke thx for the info. Appreciate your time.

    – Bigg_aye
    Nov 20 '18 at 0:51














0












0








0








I just recently started creating a .net web app in mvc5. I've tried to search for the answer as I usually do rather than asking a question but Im not sure how to word this but here it goes:



I have a class:



public partial class employee
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}


In which the class was created by using the Database First wizard with Entity Framework. (reasoning for the tag)



What I did was get the FistName, MiddleName, and LastName from the database. In which I successfully did with this ActionResult from my controller.



[ChildActionOnly]
public ActionResult Nav()
{
employee user;
using (DatabaseEntities context = new DatabaseEntities())
{
user = context.employees.FirstOrDefault(e => e.FirstName ="Bob");
if (!System.IO.File.Exists(Server.MapPath(user.imagefile)))
{
user.imagefile = $"/Content/Images/user/{user.FirstName[0]}.png";
}
}
return PartialView("_Nav", user);
}


Now I'm wondering if its possible, how to create another item to the class without adding a column to the database. I want to add a FormattedName variable to the class that is got after the database call to get the model (user) to include the new variable (FormattedName) that gets its value from the existing variables in the class.



I understand the wording and terminology could be off but any help would be great and thx in advance.



Edit:



In the comment section, I was informed about attributes ex.[NotMapped] which was information I definitely needed to know. But for this particular question, it doesn't work. Everything was ok until I updated the model using entity framework, the attributes were gone and have to keep adding them every model update which is not ideal for me. I went ahead and edited my database to include a (FormattedName) column so the problem is fixed. But I still would love to know if this is doable.










share|improve this question
















I just recently started creating a .net web app in mvc5. I've tried to search for the answer as I usually do rather than asking a question but Im not sure how to word this but here it goes:



I have a class:



public partial class employee
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}


In which the class was created by using the Database First wizard with Entity Framework. (reasoning for the tag)



What I did was get the FistName, MiddleName, and LastName from the database. In which I successfully did with this ActionResult from my controller.



[ChildActionOnly]
public ActionResult Nav()
{
employee user;
using (DatabaseEntities context = new DatabaseEntities())
{
user = context.employees.FirstOrDefault(e => e.FirstName ="Bob");
if (!System.IO.File.Exists(Server.MapPath(user.imagefile)))
{
user.imagefile = $"/Content/Images/user/{user.FirstName[0]}.png";
}
}
return PartialView("_Nav", user);
}


Now I'm wondering if its possible, how to create another item to the class without adding a column to the database. I want to add a FormattedName variable to the class that is got after the database call to get the model (user) to include the new variable (FormattedName) that gets its value from the existing variables in the class.



I understand the wording and terminology could be off but any help would be great and thx in advance.



Edit:



In the comment section, I was informed about attributes ex.[NotMapped] which was information I definitely needed to know. But for this particular question, it doesn't work. Everything was ok until I updated the model using entity framework, the attributes were gone and have to keep adding them every model update which is not ideal for me. I went ahead and edited my database to include a (FormattedName) column so the problem is fixed. But I still would love to know if this is doable.







c# asp.net-mvc entity-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 11:44







Bigg_aye

















asked Nov 20 '18 at 0:17









Bigg_ayeBigg_aye

5817




5817








  • 3





    You can use the NotMapped attribute, but its better to create a view model representing what you want in the view.

    – user3559349
    Nov 20 '18 at 0:19













  • In which class you wanted to add your new item? You can add it before return PartialView("_Nav", user);

    – Vijunav Vastivch
    Nov 20 '18 at 0:26













  • @StephenMuecke The [NotMapped] attribute worked perfectly. Thank you. But can you explain how and why is it best to create a view model representing the view for better understanding? Or would I have to post another question?

    – Bigg_aye
    Nov 20 '18 at 0:40








  • 1





    Start with What is ViewModel in MVC?. And if you editing data, it should be mandatory to use a view model (some of the reasons are explained in that link, but there are others such as [What does it mean for a property to be [Required] and nullable?](stackoverflow.com/questions/43688968/…)

    – user3559349
    Nov 20 '18 at 0:45











  • @StephenMuecke thx for the info. Appreciate your time.

    – Bigg_aye
    Nov 20 '18 at 0:51














  • 3





    You can use the NotMapped attribute, but its better to create a view model representing what you want in the view.

    – user3559349
    Nov 20 '18 at 0:19













  • In which class you wanted to add your new item? You can add it before return PartialView("_Nav", user);

    – Vijunav Vastivch
    Nov 20 '18 at 0:26













  • @StephenMuecke The [NotMapped] attribute worked perfectly. Thank you. But can you explain how and why is it best to create a view model representing the view for better understanding? Or would I have to post another question?

    – Bigg_aye
    Nov 20 '18 at 0:40








  • 1





    Start with What is ViewModel in MVC?. And if you editing data, it should be mandatory to use a view model (some of the reasons are explained in that link, but there are others such as [What does it mean for a property to be [Required] and nullable?](stackoverflow.com/questions/43688968/…)

    – user3559349
    Nov 20 '18 at 0:45











  • @StephenMuecke thx for the info. Appreciate your time.

    – Bigg_aye
    Nov 20 '18 at 0:51








3




3





You can use the NotMapped attribute, but its better to create a view model representing what you want in the view.

– user3559349
Nov 20 '18 at 0:19







You can use the NotMapped attribute, but its better to create a view model representing what you want in the view.

– user3559349
Nov 20 '18 at 0:19















In which class you wanted to add your new item? You can add it before return PartialView("_Nav", user);

– Vijunav Vastivch
Nov 20 '18 at 0:26







In which class you wanted to add your new item? You can add it before return PartialView("_Nav", user);

– Vijunav Vastivch
Nov 20 '18 at 0:26















@StephenMuecke The [NotMapped] attribute worked perfectly. Thank you. But can you explain how and why is it best to create a view model representing the view for better understanding? Or would I have to post another question?

– Bigg_aye
Nov 20 '18 at 0:40







@StephenMuecke The [NotMapped] attribute worked perfectly. Thank you. But can you explain how and why is it best to create a view model representing the view for better understanding? Or would I have to post another question?

– Bigg_aye
Nov 20 '18 at 0:40






1




1





Start with What is ViewModel in MVC?. And if you editing data, it should be mandatory to use a view model (some of the reasons are explained in that link, but there are others such as [What does it mean for a property to be [Required] and nullable?](stackoverflow.com/questions/43688968/…)

– user3559349
Nov 20 '18 at 0:45





Start with What is ViewModel in MVC?. And if you editing data, it should be mandatory to use a view model (some of the reasons are explained in that link, but there are others such as [What does it mean for a property to be [Required] and nullable?](stackoverflow.com/questions/43688968/…)

– user3559349
Nov 20 '18 at 0:45













@StephenMuecke thx for the info. Appreciate your time.

– Bigg_aye
Nov 20 '18 at 0:51





@StephenMuecke thx for the info. Appreciate your time.

– Bigg_aye
Nov 20 '18 at 0:51












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384470%2fadd-variable-to-entity-framework-created-model-class-without-editing-the-actual%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384470%2fadd-variable-to-entity-framework-created-model-class-without-editing-the-actual%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents