fluentNHibernate mapping - wiele do wielu

0

hej,
nie umiem dobrze zrozumieć mapowania wiele-do-wielu. Chodzi mi o konkretny przykład: mam tabele Wynajem:

public virtual int Id { get; set; }
        public virtual string Uwagi { get; set; }
        public virtual DateTime WynajemOd { get; set; }
        public virtual DateTime WynajemDo { get; set; }

        public virtual User User { get; set; }

i teraz chciałbym zrobić relacje wiele do wielu do tabeli Sprzet:

 public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Serial { get; set; }
        public virtual string Uwagi { get; set; }
        public virtual string BarCode { get; set; }
        public virtual Group Group { get; set; }
        public virtual Kategoria Kategoria { get; set; }

Próbowałem to zrobić tak:
do tabeli sprzęt wstawiłem:

public virtual IEnumerable<Wynajem> Wynajemy{ get; set; }

map:

this.HasManyToMany(x=>x.Wynajemy);

a do tabeli wynajem:

public virtual IEnumerable<Sprzet> Sprzety{ get; set; }

map:

this.HasManyToMany(x=>x.Sprzety);

ale jakoś to nie chce działać, dostaje błąd mówiący o tym że nie można znaleźć odpowiednich Id.

0

mam tabele [...]

To nie tabela.

relacje wiele do wielu do

http://pl.wikipedia.org/wiki/Relacja_%28matematyka%29

Wynajemy

Wtf? I dlaczego w ogole piszesz kod po polsku?

class Foo {
  public virtual int Id { get; set; }
  public virtual ISet<Bar> Bars { get; set; }

  public Foo() { Bars = new HashSet<Bar>(); }
}

class Bar {
  public virtual int Id { get; set; }
  public virtual ISet<Foo> Foos { get; set; }

  public Bar() { Foos = new HashSet<Foo> }
}

class FooMapping : ClassMap<Foo> {
  public FooMapping() {
    Id() // ...
    HasManyToMany(x => x.Bars).Inverse();
  }
}

class BarMapping : ClassMap<Bar> {
  public BarMapping() {
    Id() // ...
    HasManyToMany(x => x.Foos);
  }
}

Jak nie dziala to napisz konkretnie co i wrzuc tresc wyjatku (po angielsku).

0

hmm...dobra to teraz taki przykład:
tu jest diagram bazy:
user image

zasada działania:
User wybiera Kategorie, np. "Kamery" i następnie chce wyświetlić wszystkie pozycje z tabeli "Sprzet", które mają kategorie "Kamery" i są w grupie, w której jest aktualnie zalogowanego użytkownik.

a tak u mnie wyglądają klasy (dam same już mapowania):

 public KategoriaMap()
        {
            this.Id(x => x.Id);
            this.Map(x => x.Nazwa);
        }

public class SprzetMap: ClassMap<Sprzet>
    {
        public SprzetMap()
        {
            this.Id(x => x.Id);
            this.Map(x => x.Name);
            this.Map(x => x.BarCode);
            this.Map(x => x.Serial);
            this.Map(x => x.Uwagi);

            this.References(x => x.Group);
            this.References(x => x.Kategoria);

            this.HasManyToMany(x => x.Wynajems)
                .Table("SprzetWynajem")
                .ChildKeyColumn("Wynajem_Id")
                .ParentKeyColumn("Sprzet_Id");
        }
    }


 public GroupMap()
        {
            this.Id(x => x.Id);
            this.Map(x => x.Name);
            this.HasManyToMany(x => x.Users)
                .Table("UserGroup")
                .ParentKeyColumn("Group_Id")
                .ChildKeyColumn("User_Id")
                .Inverse();
        }

public UserGroupMap()
        {
            this.Id(x => x.Id);
            this.References(x => x.Group);
            this.References(x => x.User);
        }


 public UserMap()
        {
            this.Id(x => x.Id);
            this.Map(x => x.Email);
            this.Map(x => x.Password);
            this.Map(x => x.Salt);
            this.Map(x => x.Name);
            this.Map(x => x.IsActive);

            this.References(x => x.Role);

            this.HasManyToMany(x => x.Groups).Table("UserGroup").ChildKeyColumn("Group_Id").ParentKeyColumn("User_Id");
        }

coś tutaj po prostu jest nie halo... nie działa to poprawnie :( i nie umiem dojść gdzie jest błąd, moim zdaniem po prostu się zamotałem chyba w tych relacjach.

Oraz 2 pytanie:
załóżmy że mam tabele Wynajem oraz Sprzet, i teraz chciałbym aby jeden Wynajem miał wiele Sprzętów że tak powiem, to jaka to będzie relacja? Skoro trzeba pamiętać że jeden Sprzęt może byc wiele razy wynajmowany...oczywiście nie w tym samym czasie.

Klasy:

public class Kategoria
    {
        public virtual int Id { get; set; }
        public virtual string Nazwa { get; set; }
    }
 public class Sprzet
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Serial { get; set; }
        public virtual string Uwagi { get; set; }
        public virtual string BarCode { get; set; }
        public virtual Group Group { get; set; }
        public virtual Kategoria Kategoria { get; set; }
        public virtual IEnumerable<Image> Images { get; set; }
        public virtual IEnumerable<Wynajem> Wynajems{ get; set; }
    }

    public class Group
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual IEnumerable<User> Users { get; set; } 
    }


    public class UserGroup
    {
        public virtual int Id { get; set; }
        public virtual Group Group { get; set; }
        public virtual User User { get; set; }
    }


    public class User
    {
        public virtual int Id { get; set; }
        public virtual string Email { get; set; }
        public virtual string Salt { get; set; }
        public virtual string Password { get; set; }
        public virtual string Name { get; set; }
        public virtual bool IsActive { get; set; }

        public virtual Role Role { get; set; }

        public virtual IEnumerable<Group> Groups { get; set; }

        public virtual void AddGroup(Group g)
        {
            var list = this.Groups as IList<Group>;
            list.Add(g);
        }

        public virtual void DeleteGroup(Group g)
        {
            var list = this.Groups as IList<Group>;
            list.Remove(g);
        }
    }

0

Po co Id w UserGroup?

0

wrzóciłem do wcześniejszego posta.

1 użytkowników online, w tym zalogowanych: 0, gości: 1