0

I've created the following domain classes:

public class Car
{
    public virtual int Id { get; set; }
    public virtual string Registration { get; set; }
    public virtual User ResponsibleContact { get; set; }

    protected Car()
    {}

    public Fahrzeug(User responsibleContact, string registration)
    {
        ResponsibleContact = responsibleContact;
        Registration = registration;

        ResponsibleContact.Cars.Add(this);
    }
}

public class User
{
    public virtual int Id { get; set; }
    public virtual byte[] EncryptedUsername { get; set; }
    public virtual byte[] EncryptedPassword { get; set; }

    public virtual IList<Car> Cars { get; private set; }

    public virtual string Username
    {
        get
        {
            var decrypter = UnityContainerProvider.GetInstance().UnityContainer.Resolve<IRijndaelCrypting>();
            return decrypter.DecryptString(EncryptedUsername);
        }
    }

    protected User()
    { }

    public User(byte[] encryptedUser, byte[] encryptedPassword)
    {
        Cars = new List<Car>();
        EncryptedUsername = encryptedUser;
        EncryptedPassword = encryptedPassword;
    }
}

and the mapping classes:

public class CarMap : ClassMap<Car>
{
    public CarMap()
    {
        Id(c => c.Id).GeneratedBy.Native();
        Map(c => c.Registration);

        References(c => c.ResponsibleContact).Not.Nullable();
    }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(st => st.Id).GeneratedBy.Native();
        Map(st => st.EncryptedUsername).Column("Username");
        Map(st => st.EncryptedPassword).Column("Password");
        HasMany(st => st.Cars).Inverse().AsBag();
    }
}

If I query some Member objects, I get the members, but the cars collection is empty! If I query some Cars I got all the cars with the right Member. But within the member, the cars collection is also empty!

Is there anybody who has an Idea of what can happened?

Firo
  • 30,626
  • 4
  • 55
  • 94

1 Answers1

1

you have to make sure the foreign key column of the collection and the reference is the same otherwise there is a mismatch.

References(c => c.ResponsibleContact, "ResponsibleContact_id").Not.Nullable();

and

HasMany(st => st.Cars).Inverse().KeyColumn("ResponsibleContact_id");
Firo
  • 30,626
  • 4
  • 55
  • 94