0

У меня есть три класса Product, Stock, StockId. Stock имеет составной идентификатор Token и InternalCode, эти два свойства инкапсулированы в новый класс StockID.Как сопоставить ссылку на CompositeId с использованием Fluent NHibernate

Мои классы определения:

public class Producto 
{ 
    public virtual long Id { get; set; 
    public virtual Stock Stock { get; set; } 

    ... Some other (not so important) properties ... 

    public Producto() 
    { 
     ... 
    } 
} 

public class Stock 
{ 
    public virtual StockID ID { get; set; } 
    public virtual Producto ProductoStock { get; set; } 
    ... other properties ... 
} 

public class StockID 
{ 
    public virtual string Token { get; set; } 
    public virtual long CodigoInterno { get; set; } 

    public override int GetHashCode() 
    { 
     int hash = GetType().GetHashCode(); 
     hash = (hash * 31)^CodigoInterno.GetHashCode(); 
     hash = (hash * 31)^Token.GetHashCode(); 

     return hash; 
    } 

    public override bool Equals(object obj) 
    { 
     var other = obj as StockID; 

     if (ReferenceEquals(null, other)) return false; 
     if (ReferenceEquals(this, other)) return true; 

     return this.CodigoInterno == other.CodigoInterno && 
       this.Token == other.Token; 
    } 
} 

И эти карты:

public class ProductoMap : ClassMap<Producto> 
{ 
    public ProductoMap() 
    { 
     Id(x => x.Id); 
     // ... Other Maps and References 

     References<Stock>(p => p.Stock); 
    } 
} 

public class StockMap : ClassMap<Stock> 
{ 
    public StockMap() 
    { 
     CompositeId(stock => stock.ID) 
      .KeyProperty(x => x.CodigoInterno) 
      .KeyProperty(x => x.Token); 

     // ... Other Maps and References 
     References(x => x.ProductoStock); 
    } 
} 

это исключение я получаю ...

Внешний ключ (FKD33BD86ADE26BE17: Producto [Stock_id])) должно иметь такое же количество столбцов, что и ссылочный первичный ключ (Stock [CodigoInterno, Token])

Как это исправить?

ответ

0

Вы должны сопоставить свойство, соответствующее вашему классу Producto, с вашим классом Stock. Он должен выглядеть следующим образом:

public class StockMap : ClassMap<Stock> 
{ 
    public StockMap() 
    { 
     CompositeId(stock => stock.ID) 
      .KeyProperty(x => x.CodigoInterno) 
      .KeyProperty(x => x.Token); 
     // ... Other Maps and References 
     //This is ok since you have (i believe) a column in 
     //in your table that stores the id from Producto 
     References(x => x.ProductoStock).Column("idProducto"); 
     //Maybe you have to put the name of the column with 
     //the id of Producto in your table Stock because 
     //you could use it later. 

    } 
} 

И класс ProductMap:

public class ProductoMap : ClassMap<Producto> 
{ 
    public ProductoMap() 
    { 
     Id(x => x.Id); 
     // ... Other Maps and References 
     //If you have the id from Stock in your Stock class 
     //this will work. 
     //References<Stock>(p => p.Stock); 
     //If you don't have it, this will work: 
     HasOne<Stock>(x => x.Stock).PropertyRef("Name of the column whit the product id in the Stock table"); 
    } 
} 

Второй вариант, кажется, лучше с моей точки зрения. В любом случае, если у вас есть устаревшая база данных, то что-то вроде этого не очень большая разница.