¿Cómo puedo crear uno a al menos un mapeo fluido con varias tablas?

I have an old designed database schema, and I want map it with Fluent-Nhibenrate.

El esquema se parece a esto:

Table:Users
ID Number PK

Table:Kiosk
User_ID Number PK, FK -> Users.ID
Xml Varchar

Table:Email
User_ID Number PK, FK -> Users.ID
Update_Date Date

And I want to achieve with the mapping objects that looks something like this:

public class User
{
    private virtual Email _email {get;set;}

    public virtual int Id {get;set;}
    public virtual string Email
    {
        get
        {
            return _email.Email;
        }
        set
        {
            _email.Email = value;
            _email.UpdateDate = DateTime.Now();
        }
    }

    public virtual Kiosk Kiosk {get;set;}

    public virtual bool HasKiosk
    {
        get
        {
            return (Kiosk != null);
        }
    }
}

public class Kiosk
{
    public virtual string XmlKiosk {get;set;}
}

public class Email
{
    public virtual string Email {get;set;}
    public virtual DateTime UpdateDate {get;set;}
}

¿Alguna idea?

Thanks ahead, Amir.

preguntado el 03 de mayo de 12 a las 15:05

1 Respuestas

Usar este artículo I achieved what I wanted with tiny modifications in "Kiosk" and "Email" classes...

Aquí está el resultado:

public class Kiosk
{
    private int UserId {get;set;}
    private User User {get;set;}

    protected Kiosk();

    public Kiosk(User user)
    {
        User = user;
    }

    public virtual string XmlKiosk {get;set;}
}

public class Email
{
    private int UserId {get;set;}
    private User User {get;set;}

    protected Email();

    public Email(User user)
    {
        User = user;
    }   

    public virtual string Email {get;set;}
    public virtual DateTime UpdateDate {get;set;}
}

And the mapping looks like this:

    Public class UserMapping : IAutoMappingOverride<User>
    {
        public void override Map(AutoMapping<User> mapping)
        {
            mapping.Id(o => o.Id) // Not needed, already mapped with auto mapper, just for the example...

            mapping.HasOne(o => o.Kiosk)
                .Cascade.All();
            mapping.HasOne(Reveal.Member<User, Email>("_email"))
                .Cascade.All();

            mapping.IgnoreProperty(o => o.HasKiosk); // Simple getter, no mapping needed here.
        }
    }

    public class KioskMapping : IAutoMappingOverride<Kiosk>
    {
        public void override Map(AutoMapping<Kiosk> mapping)
        {
            mapping.Id(Reveal.Member<Kiosk>("UserId"), "ColumnName");

            mapping.HasOne<User>(Reveal.Member<Kiosk, User>("User")
                .Constrained()
                .ForeignKey();
        }
    }

    public class EmailMapping : IAutoMappingOverride<Email>
    {
        public void override Map(AutoMapping<Email> mapping)
        {
            mapping.Id(Reveal.Member<Email>("UserId"), "ColumnName");

            mapping.HasOne<User>(Reveal.Member<Email, User>("User")
                .Constrained()
                .ForeignKey();
        }
    }       

Works like a charm, Thanks everyone.

contestado el 06 de mayo de 12 a las 12:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.