0

I'm working on simple ASP .NET MVC web app which using Entity Framework ( DB First ).

First functionality I'm working on is "Room Booking". For that, I have simple model:

public partial class Room
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Color { get; set; }
}

public partial class Reservation
{
    public int Id { get; set; }
    public int RoomId { get; set; }
    public int UserId { get; set; }
    public System.DateTime Start { get; set; }
    public System.DateTime Stop { get; set; }
    public string Description { get; set; }
}

public partial class User
{
    public int ID { get; set; }
    public string UserName { get; set; }
    public string FullName { get; set; }
}

I'm trying to create View like UserReservations, but I don't know how to get User.UserName and Room.Name in Reservation.

@foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.RoomId)
                //Here I want to have Room Name instead of RoomId.
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Start)
            </td>
            <td>
                @Html.DisplayFor(modelitem => item.Stop)
            </td>
            <td>

            </td>
        </tr>
    }

How to get Room.Name in Reservation model ? Do I have to modyify it ( I think there would be modyfications in the future, so model will be updated from database and all of those changes would be discarded).

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
fanarek
  • 367
  • 3
  • 16
  • You will need to create another model that contains properties that your join query is returning. – It's a trap Apr 08 '19 at 07:36
  • if your models are configured correctly it should work like this: `reservationObject.Room.Name` and the correct way should be `public partial class Reservation{ public int RoomId { get; set; } public Room Room {get;set;}}` – Aarif Apr 08 '19 at 08:24
  • @Aarif model comes from database. If there would be any changes on database, app model will change itself too. Am i right ? – fanarek Apr 08 '19 at 08:45
  • I think in this case adding a property `public Room Room {get; set;}` doesn't change anything on schema level it just provides us some ease at the code level, the relationship remains the same, have you tried it though? – Aarif Apr 08 '19 at 08:50
  • @Aarif Yes I have. It causes errors saying that `Room` is not part of the current context, which I'm currently working with. – fanarek Apr 08 '19 at 09:06
  • @Aarif I've just created my own class which has properties of type `Room`,`User` and `Reservation` and it works. Thanks for advice. – fanarek Apr 08 '19 at 09:16
  • @fanarek Please add your controller method code to the question. This is a very simple problem. – TanvirArjel Apr 08 '19 at 09:33
  • You can view my answer here https://stackoverflow.com/questions/51044937/how-to-use-2-models-in-a-view/51045717#51045717 . You need to use MVVM pattern in this scenario –  Apr 08 '19 at 14:35

2 Answers2

0

you can add manual navigation property to Reservation (add navigation property User and Room in Reservation), after that you have a user, room property to call User.UserName and Room.Name from Reservation : [Entity Framework - Add Navigation Property Manually

hassan.ef
  • 1,300
  • 2
  • 11
  • 19
0

You can use foreign key in your database.If you use foreign key between Reservation->RoomId and Room->Id, your Reservation class is like above.

public partial class Reservation
{
    public int Id { get; set; }
    public int RoomId { get; set; }
    public int UserId { get; set; }
    public System.DateTime Start { get; set; }
    public System.DateTime Stop { get; set; }
    public string Description { get; set; }
    public virtual Room Room{ get; set; } //your foreign table
}

And so you can use Reservation.Room.Name in your view

ahmeticat
  • 1,899
  • 1
  • 13
  • 28