Reputation: 142
I've searched many questions here, that sounded similar to mine, but none if it matched my problem, so I hope for some help in this ticket..
I use EF Code First and try to map it to an existing (legacy) MySQL Database. Works fine on my properties except for one, and I can't figure out why..
Model that is null:
[Table("einheitenstamm")]
public class Unit : ClassicEntity
{
[Key]
[Column("estID")]
public int Id { get; set; }
[Column("estEinheit")]
public string Name { get; set; }
}
Model containing the reference to Unit:
[Table("artikeldaten_preise")]
public class ArticlePrice : ClassicEntity
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("einheit")]
[ForeignKey("Id")]
public virtual Unit Unit { get; set; } /* is always null!!!!! */
[Column("preisliste")]
[ForeignKey("Id")]
public virtual Pricelist Pricelist { get; set; } /* gets loaded without problems */
[Column("artikel")]
[ForeignKey("Id")]
public virtual Article Article { get; set; } /* gets loaded without problems */
[Column("preis")]
public double Price { get; set; }
}
Database Table Create:
CREATE TABLE `artikeldaten_preise` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`preisliste` INT(11) NOT NULL,
`artikel` VARCHAR(10) NOT NULL,
`preis` DECIMAL(10,2) NOT NULL,
`einheit` INT(11) NOT NULL,
`changed` DATETIME NULL DEFAULT NULL,
`sys_deleted` BIT(1) NOT NULL DEFAULT b'0',
`sys_changedfrom` VARCHAR(50) NULL DEFAULT NULL,
`sys_changedat` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `artikel` (`artikel`) USING BTREE,
INDEX `preisliste` (`preisliste`) USING BTREE,
INDEX `einheit` (`einheit`) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2;
Code:
var units = from unit in context.Units
where unit.Id == 4
select unit;
foreach (var unit in units)
Console.WriteLine(unit.Id + ": " + unit.Name); /* works */
var prices = from price in context.ArticlePrices
select price;
foreach (var price in prices.ToList()) /* price.Unit = NULL ........... */
MessageBox.Show(price.Article.Description + " " + price.Price + "/"
+ price.Unit.Name + " in pricelist '"
+ price.Pricelist.Name + "'");
Can anybody please tell me what I do wrong?
Upvotes: 2
Views: 910
Reputation: 158
You have to define the relations explicitly and create an xxId property for each navigation property that you can then decorate with the corresponding ForeignKey attribute. No need for "real" foreign keys here..
[Table("artikeldaten_preise")]
public class ArticlePrice : ClassicEntity
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("einheit")]
public int UnitId { get; set; }
[ForeignKey("UnitId")]
public virtual Unit Unit { get; set; }
[Column("preisliste")]
public int PricelistId { get; set; }
[ForeignKey("PricelistId")]
public virtual Pricelist Pricelist { get; set; }
[Column("artikel")]
public int ArticleId { get; set; }
[ForeignKey("ArticleId")]
public virtual Article Article { get; set; }
[Column("preis")]
public double Price { get; set; }
}
Upvotes: 1
Reputation: 4803
var prices = from price in context.ArticlePrices.Include("Price.Unit")
select price;
Upvotes: 0