I have the following entities.
public class Country{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual IList<City> Cities { get; set; }
}
public class City{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual Country Country {
get { return _country; }
}
private readonly Country _country;
}
But given the following code a user can do
- _country.Name = "newCountry";
- _country.Cities.Add(AnotherCity);
- _country.Cities[0].Name = "newCity" 4. _country.Cities[0].Country.Name = "Opps"
1-3 are ok but my problems are with #4. There is some need for me make this as a bi-directional One-Many mapping, But I would ideally wish that the reference of Country (including all its properties) in city entity be readonly. Is there a way I can do that? I tried
public interface IReadOnlyCountry {
int Id { get; }
string Name { get; }
IList<City> Cities { get; }
}
public class Country : IReadOnlyCountry{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual IList<City> Cities { get; set; }
}
public class City {
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual IReadOnlyCountry Country {
get { return _country; }
}
private readonly Country _country;
}
But its not working for Nhibernate (not sure if I am doing something wrong, or this is not possible). How do you handle such situations?