Question

I'm working with EF 5, Web Api and MVC4 i need help about this database design. I need save in database the city and town of a hospital, but without cycles, a hospital has one city and town, and a city has many towns

public class City
{
    public int CityId { get; set; }
    public List<Town> Towns { get;  set; }
    public string Name { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }

    public City()
    {
        Towns = new List<Town>();
    }
}

 public class Town
{
    public int TownId { get; set; }

    public int CityId { get; set; }

    public string Name { get; set; }

    public byte[] RowVersion { get; set; }
}

 public class Hospital
{
    public int HospitalId { get; set; }

    [Required]
    public string Name { get; set; }

    public int TownId { get; set; }
    public  Town Town { get; set; }

    public int CityId { get; set; }

    public City City { get; set; }

    public bool IsAvailable { get; set; }

    [Timestamp]
    public byte[] RowVersion { get; set; }


    public Hospital()
    {
      this.IsAvailable = true;
    }
}

I don't like the property City in Hospital class, but i need another option, it's like a cycle

Was it helpful?

Solution

What's wrong with the "cycle"? EF uses them all the time, and bi-directional navigation is quite useful! Of course, it breaks when trying to serialize, but you shouldn't be doing that anyways :).

As to your question, if a City "has" multiple Towns, and a Hospital is in a town, then there is no need for a Hospital to know about a City. It knows what town it is in, and thus what City it is in. Of course, if a given Town can be in more than one City, (but the Hospital is in one unique pair) then your design looks perfect.

With the information you gave, I would just remove the "City" property from the Hospital class altogether. If you need to access it, just go through the Town property.

myHospital.Town.City

OTHER TIPS

Well, if each Town is related to a single City, and each Hospital is related to a single Town, then you can use these relations to find the Hospital's City easily.

You don't need to have the City as a column in the Hospital table in the database, however you might want to still have it as a property in the code for simplicity.
in that case, the easy way to do it is to connect the Hospital class to a view rather then the actual Hospital table.

If I would write that application, I would take classes like this.

City does not contain any relation property:

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }
}

Town is in any City

public class Town
{
    public int Id { get; set; }
    public int CityId { get; set; }
    public string Name { get; set; }
    public byte[] RowVersion { get; set; }
}

And Hospital is in Town. You can access City by TownId

public class Hospital
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    public int TownId { get; set; }

    //you can uncomment this for less coding for searching, filtering etc..
    //public int CityId { get; set; }

    public bool IsAvailable { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }
    public Hospital()
    {
      this.IsAvailable = true;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top