views:

86

answers:

1

I am creating a NHibenate application with one to many relationship. Like City and State data. City table

CREATE TABLE [dbo].[State](
    [StateId] [varchar](2)  NOT NULL primary key,
    [StateName] [varchar](20) NULL)
CREATE TABLE [dbo].[City](
    [Id] [int] primary  key IDENTITY(1,1) NOT NULL ,
    [State_id] [varchar](2)  NULL refrences State(StateId),
    [CityName] [varchar](50)  NULL)

My mapping is follows

public CityMapping()
{
    Id(x => x.Id);
    Map(x => x.State_id);
    Map(x => x.CityName);
    HasMany(x => x.EmployeePreferedLocations)
        .Inverse()
        .Cascade.SaveUpdate();
    References(x => x.State)
        //.Cascade.All();
        //.Class(typeof(State))
        //.Not.Nullable() 
        .Cascade.None()  
        .Column("State_id");
}

public StateMapping()
{
    Id(x => x.StateId)
        .GeneratedBy.Assigned();
    Map(x => x.StateName);
    HasMany(x => x.Jobs)
        .Inverse();
        //.Cascade.SaveUpdate();
    HasMany(x => x.EmployeePreferedLocations)
        .Inverse();

   HasMany(x => x.Cities)
       // .Inverse()
       .Cascade.SaveUpdate();
       //.Not.LazyLoad()
}

Models are as follows:

[Serializable]
public partial class City
{
    public virtual System.String CityName { get; set; }
    public virtual System.Int32 Id { get; set; }
    public virtual System.String State_id { get; set; }
    public virtual IList<EmployeePreferedLocation> EmployeePreferedLocations { get; set; }
    public virtual JobPortal.Data.Domain.Model.State State { get; set; }

    public City(){}

}

public partial class State
{
    public virtual System.String StateId { get; set; }
    public virtual System.String StateName { get; set; }
    public virtual IList<City> Cities { get; set; }
    public virtual IList<EmployeePreferedLocation> EmployeePreferedLocations { get; set; }
    public virtual IList<Job> Jobs { get; set; }
    public State()
    {
        Cities = new List<City>();
        EmployeePreferedLocations = new List<EmployeePreferedLocation>();
        Jobs = new List<Job>();
    }

    //public virtual void AddCity(City city)
    //{
    //    city.State = this;
    //    Cities.Add(city);

    //}
}

My Unit Testing code is below.

City city = new City();

IRepository<State> rState = new Repository<State>();
Dictionary<string, string> critetia = new Dictionary<string, string>();
critetia.Add("StateId", "TX");
State frState = rState.GetByCriteria(critetia);

city.CityName = "Waco";
city.State = frState;

IRepository<City> rCity = new Repository<City>();

rCity.SaveOrUpdate(city);

City frCity = rCity.GetById(city.Id);

The problem is , I am not able to insert record. The error is below.

"Invalid index 2 for this SqlParameterCollection with Count=2."

But the error will not come if I comment State_id mapping field in the CityMapping file. I donot know what mistake is I did. If do not give the mapping Map(x => x.State_id); the value of this field is null, which is desired. Please help me how to solve this issue.

+1  A: 

Few remarks:

  1. Remove this State_id property from the City class and the mapping. You already have a State property so it makes no sense in your object model.

  2. Those Jobs and EmployeePreferedLocations properties in the State class don't have any related columns/tables in your database (at least the one you've shown here) while you have mappings for them.

Darin Dimitrov
Point #1 is the root cause of the error: state_id is mapped twice in the same class.
Jamie Ide
Hi Jamie/ Darin If I don't put the sate_Id in the City table, then how can I get the state_id when I fetch city table? I don't want to fetch State table while I fetch city table.
Jit
Assuming you have a city you can navigate through the association: `city.State.StateId`.
Darin Dimitrov
Darin,I agree with you on this point. But Let's say I want to fetch based on sate( say State_id =TX). If put it in the query as a parameter, I get the mapping error as below. ex.Message "could not resolve property: State_id of: JobPortal.Data.Domain.Model.City" stringI am putting the code belowIRepository<City> rCity = new Repository<City>(); Dictionary<string, string> critetia = new Dictionary<string, string>(); critetia.Add("State_id", "TX"); City frCity = rCity.GetByCriteria(critetia);
Jit