1

I am using the .NetCore Entity Framework for the first time and want to know if the it is possible to generate a custom model.

When setting up the app, EF created all the models from the database. That is fine and expected.

However, I now created a new controller that returns data that is the result of a complicated linq query.

All my other controllers return a model like this:

return View(characterList);

where characterList is an actual model of a database table.

But how would I create a brand new custom model that does not represent any table in the database?

Thanks!

4
  • 1
    In that line of code characterList is being used as the view-model for the view that matches the controller action. There really isn't anything special about the kind of class that you can use there. You can create a brand new class to act as your view-model with whatever properties on it that you need to display your page correctly. Commented Nov 7, 2018 at 19:37
  • @BradleyUffner thanks! All my other models have views associated with them. So I can just create a custom class with the needed properties and then create a custom view for that class? Commented Nov 7, 2018 at 19:42
  • 2
    That's the general idea.The answer @wegelagerer gave is pretty much spot-on. Commented Nov 7, 2018 at 19:45
  • Your classes used with EF are entities. You might some times use them as the "model" of a view, but the two are not inseparable. You can have entity classes that are never used as models and models that have nothing to do with your database or EF. Commented Nov 7, 2018 at 20:40

2 Answers 2

3

You would first simply create the model you want to have in your code. For example:

Public class NewModel {
Public String Test {get; set;}
}

Then you can use your context and the power of linq/select to query in your new model. Something like this:

List<NewModel> list = dbContext.Set<OldModel>().Where(...).Select<NewModel>(x=> new NewModel(){ Test = x.OldTestString }).ToList()

And so you get a list of the new model. You could e.g. include other tables and join them in the query to make it more complicated. But this example should give you a starting point.

Sign up to request clarification or add additional context in comments.

Comments

2

If the model you are explaining is supposed to be used only for the views consider creating a ViewModel which is basically a class that contains only the properties needed for the view usually without any logic or only with a logic immediatelly necessary for displaying in a view.

For example, you'd create a new class, let's say CharacterVM

public class CharacterVM
{
    public string Name{ get; set; }

    public string CharacterType {get; set; }

    public bool Invincible{ get; set; }
}

In your view you'd use CharacterVM which has all the properties exposed in the CharacterVM class

@model CharacterVM

The most important step is remapping the properties from your database model (let's say it is called Character) where all you have to do in that case is to remap the properties of the Character to the properties of the new instance of CharacterVM you'd pass to the view.

public IActionResult Index(int idCharacter)
{
    var character = db.Characters.SingleOrDefault(c => c.idCharacter == idCharacter);

    var characterVM = new CharacterVM()
    {
        Name = character.Name,
        CharacterType = character.Type.Name,
        Invincibility = false
    };

    return View(characterVM);
}

2 Comments

This new, custom model would have a set of of properties in which some do exist in a database table(and hence existing model) and some don't exist in the database. Thinking about the custom model in my head right now, it will have 2 properties from the characterList model, 2 properties from the gameWorld model, 1 property from the playerGroup model, and 6 custom properties.
@SkyeBoniwell It doesn't matter where the properties come from; you are free to combine them into the ViewModel as you wish. I added an example of the controller. I hope it clarifies what should be done in order to achieve what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.