Do not create a table in BD based on the model in MVC 5

1

I'm working with ASP.NET MVC and I need to have it show me a list of registered users, although I do not want them to be saved in the database, but I get them from the RegisterViewModel model that was created automatically when the project was created , I have a model called UserPermission in which I only have the object to make the list, and I put it as PrimaryKey :

public class UserPermission  
{
    [Key]
    public String UserId { get; set; }
    public virtual ApplicationUser User { get; set; }
}

In my controller I have the following:

public class UserPermissionsController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();

    // GET: UserPermissions
    public ActionResult Index()
    {
        var Users = db.Users;
        List<UserPermission> UserPermissions = new List<UserPermission>();
        foreach (var User in Users)
        {
            UserPermission permissions = new UserPermission();
            permissions.User = User;
            permissions.UserId = User.Id;
            UserPermissions.Add(permissions);
        }
        return View(UserPermissions);
    }
}

Which shows the list of users in Index :

@model SigaWeb.Models.IndexPaginatorViewModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Crear nuevo", "Create")
</p>
<table class="table table-striped">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.UserPermissions[0].User.Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UserPermissions[0].User.UserName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model.UserPermissions) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.User.Id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.User.UserName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.UserId }) |
            @*@Html.ActionLink("Details", "Details", new { id=item.UserId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.UserId })*@
        </td>
    </tr>
}

</table>
@{Html.RenderPartial("_paginator", Model);}

Well, the fact is that I do not want to save anything in the database, it would simply serve to show me, but at the moment of adding the [Key] to my model, and wanting to execute it, it asks me to perform a migration which does not I want to do it.

Is there any way I can show it without it being stored in the database, and if so, can you tell me how?

    
asked by Karla 02.08.2017 в 22:19
source

1 answer

0

When you use the word virtual you are creating a one to many relationship between the classes, which generates a change in the model and that is why you get the message to execute the migration. Simply remove the virtual word in the UserPermission class declaration like this:

public class UserPermission  
{
    [Key]
    public String UserId { get; set; }
    public ApplicationUser User { get; set; }
}
    
answered by 19.09.2017 в 22:56