1

I been working on a database in entity framework but encountering some problems.

I have a HotelChain model like this:

[Key]
public int HotelChainID { get; set; }
public string Name { get; set; }
public List<Hotel> Hotels { get; set; }

and a Hotel model:

[Key]
public int HotelID { get; set; }
public string Name { get; set; }
public List<Building> Buildings { get; set; }
public List<Amenities> Amenities { get; set; }

My HotelInitializer class:

        Repository.InsertHotel(new Hotel("Brussels", Repository.GetAllBuildings(), Repository.GetAllAmenities()));
        Repository.InsertHotel(new Hotel("Paris", Repository.GetAllBuildings(), Repository.GetAllAmenities()));
        Repository.InsertHotel(new Hotel("Berlin", Repository.GetAllBuildings(), Repository.GetAllAmenities()));
        Repository.InsertHotel(new Hotel("London", Repository.GetAllBuildings(), Repository.GetAllAmenities()));
        Repository.InsertHotel(new Hotel("Tenerife", Repository.GetAllBuildings(), Repository.GetAllAmenities()));
        Repository.InsertHotel(new Hotel("Amsterdam", Repository.GetAllBuildings(), Repository.GetAllAmenities()));
        Repository.InsertHotelChain(new HotelChain("Company Name", Repository.GetAllHotels()));

The problem is that there are double records in my Hotel table in the database, one time with the HotelChain_HotelChainID on null and one time with the HotelChain_HotelChainD on 1. enter image description here

I'd like to achieve that only the records with the Hotelchain ID on 1 get added to the table. How could I achieve this?

These are my Repository methods:

    public static int InsertHotel(Hotel hotel)
    {
        HotelDbContext context = new HotelDbContext();
        context.Hotels.Add(hotel);
        context.SaveChanges();
        return hotel.HotelID;
    }
    public static List<Hotel> GetAllHotels()
    {
        HotelDbContext context = new HotelDbContext();
        return context.Hotels.ToList();
    }

Thank you for your time!

5
  • 1
    Seems like your Hotel class is missing a foreign key reference to HotelChain. In your initializer class you shouldn't be able to create hotels unless you have a hotel chain that exists. You should first add a foreign key reference first (as per your business logic - can a hotel exist in your system that's not part of a chain?). If you leave it out, you can still achieve what you're trying to do by first creating a hotel chain and then adding a list of hotels to that chain. Commented May 26, 2016 at 19:46
  • @RizJa For my business logic, a hotel should not be able to exist that is not part of a hotelchain, It is no longer listing null values now with this code: pastebin.com/Wc1W0nWV I also believe I will have to do tesame with the Buildings, Floors ... if I am correct? Thank you for the help! Commented May 27, 2016 at 7:46
  • Yes, the same logic would apply to Buildings and Floors.. a floor should only belong to a single building so you'd need a foreign key reference there as well. And not a problem.. you can either answer your own question and mark it as solved or I can put the comment above as an answer and you can accept that. Commented May 27, 2016 at 16:02
  • You can put the comment above and I will mark it as answered :) Commented May 27, 2016 at 16:07
  • Sounds good - posted! Commented May 27, 2016 at 18:20

1 Answer 1

1

Seems like your Hotel class is missing a foreign key reference to HotelChain. In your initializer class you shouldn't be able to create hotels unless you have a hotel chain that exists. You should first add a foreign key reference first (as per your business logic - can a hotel exist in your system that's not part of a chain?). If you leave it out, you can still achieve what you're trying to do by first creating a hotel chain and then adding a list of hotels to that chain.

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

Comments

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.